﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Data.Common;
using System.Text;
using System.Data;
using System.Web;
using System.Reflection;
using System.Xml;
using System.Data.OleDb;
using System.IO;
using System.Web.Configuration;
using System.Security.Cryptography;
using System.IO.Compression;
using System.Threading;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Text.RegularExpressions;
using AliCMS.SystemModule;
using System.Net;
using System.Runtime.InteropServices;

namespace AliCMS
{
    public sealed partial class Manager : IManager
    {
        private void _Manager()
        {
            _initializedEvents = new EventWaitHandle[] { new ManualResetEvent(false), new ManualResetEvent(false) };
			_generateEvent = new AutoResetEvent(true);
        }

        private string _MapPath(string path)
		{
            if (HttpContext.Current != null)
                return HttpContext.Current.Server.MapPath(path == "" ? "~" : path);
            else
                return Path.Combine(_webAbsolutePath, path == "" ? "" : path.Substring(_webPath.Length));
		}
        private string _GetWebPath()
        {
            return _webPath;
        }
        private string _GetCachePath()
        {
            return _GetWebPath() + _site[Alias.InstanceConfig.CacheDirectory] + "/";
        }
        private string _GetCommonPath()
        {
            return _GetWebPath() + _site[Alias.InstanceConfig.CommonDirectory] + "/";
        }
        private string _GetDataPath(object instanceID, string lang)
        {
            object id = instanceID;
            if (instanceID is Guid)
            {
                DataRow[] rows = _channels.Select(Alias.Instance.ID + "='" + instanceID + "'");
                if (rows.Length == 0)
                    id = null;
                else
                    id = rows[0][Alias.Instance.InnerID];
            }

            if (id != null)
                return _GetWebPath() + _site[Alias.InstanceConfig.DataDirectory] + "/" + id + "/" + (String.IsNullOrEmpty(lang) ? "" : lang + "/");
            else
                return _GetWebPath() + _site[Alias.InstanceConfig.DataDirectory] + "/";
        }
        private string _GetTemplatePath(string templatePath)
		{
            string root = _GetWebPath() + _site[Alias.InstanceConfig.TemplateDirectory] + "/";
            if (String.IsNullOrEmpty(templatePath))
                return root;

            return Path.Combine(root, templatePath);
        }
        private string _GetCachedTemplatePath(string templatePath, string langAb, bool isDebug)
		{
            string debug = null;
            if (isDebug)
                debug = "debug/";

            if (String.IsNullOrEmpty(templatePath))
                return _GetWebPath() + _site[Alias.InstanceConfig.CacheDirectory] + "/" + _site[Alias.InstanceConfig.TemplateDirectory] + "/" + debug;
            else if (langAb == null)
                return _GetWebPath() + _site[Alias.InstanceConfig.CacheDirectory] + "/" + _site[Alias.InstanceConfig.TemplateDirectory] + "/" + debug + templatePath.TrimEnd('/') + "/";
            else
                return _GetWebPath() + _site[Alias.InstanceConfig.CacheDirectory] + "/" + _site[Alias.InstanceConfig.TemplateDirectory] + "/" + debug + templatePath.TrimEnd('/') + "/" + langAb + "/";
        }
        private string _GetSkinPath(DataRow channel)
        {
            return _GetSkinPath(channel[Alias.Channel.SkinPath].ToString());
        }
        private string _GetSkinPath(string skinPath)
        {
            return _GetTemplatePath(skinPath);
        }
        private string _GetCssPath(string skinPath)
		{
            return _GetSkinPath(skinPath) + "css/";
		}
        private string _GetImagePath(string skinPath)
		{
            return _GetSkinPath(skinPath) + "image/";
		}
        private string _GetJsPath(string templatePath)
		{
            string path = _GetTemplatePath(templatePath);
            if (!Directory.Exists(MapPath(path + "js/")))
                path = Path.GetDirectoryName(path.TrimEnd('/')).Replace('\\', '/') + "/";

            return path + "js/";
		}
        private string _GetModulePath(string ns)
        {
            if (ns == null)
                return _GetWebPath() + _site[Alias.InstanceConfig.ModuleDirectory] + "/";

            return _GetWebPath() + _site[Alias.InstanceConfig.ModuleDirectory] + "/" + ns + "/";
        }

        private DataRow _GetChannel(string path)
        {
            if (path.StartsWith("/debug", StringComparison.InvariantCultureIgnoreCase))
                path = path.Substring(6);

            int length = -1;
            DataRow channel = null;
            for (int i = 0; i < _channels.Rows.Count; i++)
            {
                bool isMatch = false;
                string p = _channels.Rows[i][Alias.Channel.Path].ToString();
                if (p.IndexOf('{') != -1)//包含正则表达式情况
                {
                    p = "^" + Regex.Replace(p, @"\{\w+:(.*?)(?<!\\)\}", "$1").Replace(@"\}", "}");
                    Match match = Regex.Match(path, p);
                    if (match.Success)
                        p = match.Value;
                    isMatch = match.Success;
                }
                else
                {
                    isMatch = path.StartsWith(p, StringComparison.InvariantCultureIgnoreCase);
                }

                if (isMatch)
                {
                    if (channel == null || length < p.Length)
                    {
                        length = p.Length;
                        channel = _channels.Rows[i];
                    }
                }
            }

            return channel;
        }
        private DataRow _GetExtraInstance(DataRow channel, string mark)
        {
            DataRow[] rows = _extraInstances.Select(Alias.Channel.ExtraInstance.ChannelID + "='" + channel[Alias.Channel.ID] + "' AND " + Alias.Channel.ExtraInstance.Mark + "='" + mark + "'");
            if (rows.Length == 0)
            {
                string lang = null;
                if (Convert.IsDBNull(channel[Alias.Channel.TranslateLang]))
                    lang = "(" + Alias.Channel.TranslateLang + " IS NULL AND " + Alias.Channel.Lang + "='" + channel[Alias.Channel.Lang] + "' OR " + Alias.Channel.TranslateLang + " IS NOT NULL AND " + Alias.Channel.TranslateLang + "='" + channel[Alias.Channel.Lang] + "')";
                else
                    lang = "(" + Alias.Channel.TranslateLang + " IS NULL AND " + Alias.Channel.Lang + "='" + channel[Alias.Channel.TranslateLang] + "' OR " + Alias.Channel.TranslateLang + " IS NOT NULL AND " + Alias.Channel.TranslateLang + "='" + channel[Alias.Channel.TranslateLang] + "')";

                rows = _channels.Select(Alias.Module.Name + "='" + mark + "' AND " + lang);
                if (rows.Length > 0)
                    return rows[0];

                rows = _instances.Select(Alias.Module.Name + "='" + mark + "'");
                if (rows.Length > 0)
                    return rows[0];
                else
                    return null;
            }

            if (Guid.Equals(rows[0][Alias.Channel.ExtraInstance.BindingChannelID], Guid.Empty))
                rows = _instances.Select(Alias.Instance.ID + "='" + rows[0][Alias.Channel.ExtraInstance.BindingInstanceID] + "'");
            else
                rows = _channels.Select(Alias.Channel.ID + "='" + rows[0][Alias.Channel.ExtraInstance.BindingChannelID] + "'");

            if (rows.Length == 0)
                return null;
            else
                return rows[0];
        }
        private IObject _CreateObject(string objectName, int instanceID, string lang)
        {
            return _CreateObject(objectName, instanceID, lang, true, null);
        }
        private IObject _CreateObject(string objectName, int instanceID, string lang, bool checkRight)
        {
            return _CreateObject(objectName, instanceID, lang, checkRight, null);
        }
        private IObject _CreateObject(string objectName, int instanceID, string lang, bool checkRight, string database)
        {
            string assemblyName = null;
            string typeName = null;
            if (instanceID <= 0)
            {
                assemblyName = "AliCMS.SystemModule.Bll";
                instanceID = 1;
            }
            else
            {
                DataRow[] rows = _instances.Select(Alias.Instance.InnerID + "=" + instanceID);
                if (rows.Length == 0)
                    return null;

                assemblyName = rows[0][Alias.Module.Namespace] + ".Bll";
            }

            #region 创建对象实例
            object[] parameters = new object[] { instanceID, lang, checkRight, database };
            typeName = assemblyName + "." + objectName.Replace(".", "+");
            IObject result = null;

            #region 全部合并成一个dll的情况
            Type type = Type.GetType(typeName);
            if (type != null)
            {
                result = (IObject)Activator.CreateInstance(type, parameters);
            }
            #endregion

            #region 独立dll情况
            if (result == null)
                result = (IObject)Assembly.Load(assemblyName).CreateInstance(typeName, false, BindingFlags.CreateInstance, null, parameters, null, null);
            #endregion
            #endregion

            if (result is ObjectBase)
                ((ObjectBase)result).Manager = this;

            return result;
        }

        private void _MakeSureFolderExist(string path)
        {
            if (Directory.Exists(path))
                return;

            int index = path.Length - 1;
            while ((index = path.LastIndexOf('\\', index)) != -1)
            {
                if (Directory.Exists(path.Substring(0, index)))
                {
                    index++;
                    break;
                }

                index--;
            }

            while ((index = path.IndexOf('\\', index)) != -1)
            {
                if (!Directory.Exists(path.Substring(0, index)))
                {
                    try
                    {
                        Directory.CreateDirectory(path.Substring(0, index));
                    }
                    catch
                    {
                        Api.CreateDirectory(path.Substring(0, index));
                    }
                }

                index++;
            }

            if (!Directory.Exists(path))
            {
                try
                {
                    Directory.CreateDirectory(path);
                }
                catch
                {
                    Api.CreateDirectory(path);
                }
            }
        }
        private string _GenerateRandomPassword(int bitCount)
        {
            string result = "";
            Random random = new Random(DateTime.Now.Millisecond);

            for (int i = 0; i < bitCount; i++)
            {
                int r;
                int type = random.Next(3);
                switch (type)
                {
                    case 0://数字
                        r = random.Next(10);
                        result += r;
                        break;
                    case 1://a-z
                        r = random.Next(24);
                        result += (char)((int)'a' + r);
                        break;
                    case 2://A-Z
                        r = random.Next(24);
                        result += (char)((int)'A' + r);
                        break;
                }
            }
            return result;
        }

        private string _MakeClassName(string pathInTemplate)
        {
            string className = String.Empty;
            int dot = pathInTemplate.LastIndexOf('.');
            if (dot == -1)
                pathInTemplate = pathInTemplate.Replace('\\', '.').Replace('/', '.');
            else
                pathInTemplate = pathInTemplate.Substring(0, dot).Replace('\\', '.').Replace('/', '.');
            bool upper = true;
            for (int i = 0; i < pathInTemplate.Length; i++)
            {
                if (pathInTemplate[i] == '_' || pathInTemplate[i] == '.')
                {
                    if (pathInTemplate[i] == '.')
                        className += '.';

                    upper = true;
                    continue;
                }

                if (upper)
                    className += Char.ToUpper(pathInTemplate[i]);
                else
                    className += pathInTemplate[i];

                upper = false;
            }

            return className;
        }

		/*编码SQL语句Like中的通配符，适用于Access数据库*/
        private string _AccessEscapeWildcard(string source)
		{
			source = source.Replace("%", "[%]");
			source = source.Replace("_", "[_]");
			source = source.Replace("*", "[*]");
			source = source.Replace("?", "[?]");
			source = source.Replace("#", "[#]");
			source = source.Replace("[", "[[]");
			return source;
		}

        private void _InitSystem()
        {
            lock (_initializedEvents[0])
            {
                _initializedEvents[0].Reset();

                _webPath = HttpContext.Current.Request.ApplicationPath.TrimEnd('/') + "/";
                _webAbsolutePath = HttpContext.Current.Server.MapPath(_webPath);

                HttpContext.Current.Cache.Add("ClearAllCache", DateTime.Now.ToString(), null, DateTime.MaxValue, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.NotRemovable, null);

                _UpdateSite("Global", null);
                UpdateObjects();
                UpdateProperties();
                _UpdateLanguages();
                _UpdateInstances();
                _UpdateChannels();

                foreach (DataRow row in _languages.Rows)
                {
                    _UpdateSite("Basic", row[Alias.Language.Ab].ToString());
                }

                _UpdateExtraInstances();
                _UpdatePages();
                _UpdateRoles();
                _UpdatePrivileges();
                _UpdatePermissions();
                _UpdateRewrite();
				_UpdateClients();
				_UpdateWhiteList();
				_UpdateBlackList();
				_UpdateKeywordFilter();

                if (_taskThread == null)
                {
                    _UpdateTask();

                    _taskThread = new Thread(new ThreadStart(RunTask));
                    _taskThread.Start();
				}

				#region 检查阻止非法用户
				XmlDocument doc = new XmlDocument();
				string file = MapPath(GetWebPath() + "alicms.config");
				doc.Load(file);

				XmlNode xkey = doc.DocumentElement.SelectSingleNode("XKey");
				if (xkey != null)
				{
					_isForbidden = true;
					_forbiddenRedirect = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(xkey.InnerText));
				}
				#endregion

				_initializedEvents[0].Set();
            }
        }
        private void _UpdateSite(string type, string lang)
        {
            lock (_initializedEvents[1])
            {
                _initializedEvents[1].Reset();

                if (_site == null)
                    _site = new Dictionary<string, string>();

                Dictionary<string, string> config = null;
                if (type == "Global")
                {
                    using (IInstanceConfig bll = (IInstanceConfig)_CreateObject("InstanceConfig", 0, null, false))
                    {
                        config = bll.GetGlobalSettings();
                        foreach (KeyValuePair<string, string> kvp in config)
                        {
                            _site[kvp.Key] = kvp.Value;
                        }

                        _site[Alias.InstanceConfig.ConnectionString] = _site[Alias.InstanceConfig.ConnectionString].Replace("{#WebPath#}", _MapPath(_GetWebPath()));
                        _site[Alias.InstanceConfig.ConnectionString] = _site[Alias.InstanceConfig.ConnectionString].Replace("{#DataPath#}", _MapPath(_GetDataPath(null, null)));
                    }
                }
                else if (type == "Basic" && !String.IsNullOrEmpty(lang))
                {
                    using (IInstanceConfig bll = (IInstanceConfig)_CreateObject("InstanceConfig", 0, lang, false))
                    {
                        config = bll.GetBasicSettings();
                        foreach (KeyValuePair<string, string> kvp in config)
                        {
                            _site[kvp.Key + lang] = kvp.Value;
                        }
                    }
                }

                _initializedEvents[1].Set();
            }
        }
        private void _UpdateLanguages()
        {
            lock (_initializedEvents[1])
            {
                _initializedEvents[1].Reset();

                using (ILanguage bll = (ILanguage)_CreateObject("Language", 0, String.Empty, false))
                {
                    _languages = bll.List(-1, -1, null, null);
                }

                _initializedEvents[1].Set();
            }
        }
        private void _UpdateInstances()
        {
            lock (_initializedEvents[1])
            {
                _initializedEvents[1].Reset();

                using (IInstance bll = (IInstance)_CreateObject("Instance", 0, String.Empty, false))
                {
                    _instances = bll.List(-1, -1, null, null);
                }

                DataTable module = null;
                using (AliCMS.SystemModule.IModule bll = (AliCMS.SystemModule.IModule)_CreateObject("Module", 0, null, false))
                {
                    module = bll.List(-1, -1, (Dictionary<string, object>)null, (Dictionary<string, string>)null);
                }

                _instances.Columns.Add(Alias.Module.Name, typeof(string));
                _instances.Columns.Add(Alias.Module.Namespace, typeof(string));
                foreach (DataRow instance in _instances.Rows)
                {
                    instance[Alias.Instance.ConnectionString] = instance[Alias.Instance.ConnectionString].ToString().Replace("{#WebPath#}", _MapPath(_GetWebPath())).Replace("{#DataPath#}", _MapPath(_GetDataPath(null, null)));

                    DataRow[] rows = module.Select(Alias.Module.ID + "='" + instance[Alias.Instance.ModuleID] + "'");
                    if (rows.Length != 0)
                    {
                        instance[Alias.Module.Name] = rows[0][Alias.Module.Name];
                        instance[Alias.Module.Namespace] = rows[0][Alias.Module.Namespace];
                    }
                }

                _initializedEvents[1].Set();
            }
        }
        private void _UpdateChannels()
        {
            lock (_initializedEvents[1])
            {
                _initializedEvents[1].Reset();

                using (IChannel bll = (IChannel)_CreateObject("Channel", 0, String.Empty, false))
                {
                    _channels = bll.List(-1, -1, null, null);
                }

                Dictionary<string, object> template = null;
                using (ITemplate templateBll = (ITemplate)_CreateObject("Template", 0, String.Empty, false))
                {
                    _channels.Columns.Add(Alias.Instance.ID, typeof(Guid));
                    _channels.Columns.Add(Alias.Instance.InnerID, typeof(int));
                    _channels.Columns.Add(Alias.Module.ID, typeof(Guid));
                    _channels.Columns.Add(Alias.Module.Name, typeof(string));
                    _channels.Columns.Add(Alias.Module.Namespace, typeof(string));
                    _channels.Columns.Add(Alias.Template.IsAdmin, typeof(bool));
                    foreach (DataRow channel in _channels.Rows)
                    {
                        if (Guid.Equals(channel[Alias.Channel.InstanceID], Guid.Empty))
                        {
                            channel[Alias.Instance.ID] = Guid.Empty;
                            channel[Alias.Instance.InnerID] = 0;
                            channel[Alias.Module.ID] = Guid.Empty;
                            channel[Alias.Module.Name] = String.Empty;
                            channel[Alias.Module.Namespace] = String.Empty;
                        }
                        else
                        {
                            DataRow row = _instances.Select(Alias.Instance.ID + "='" + channel[Alias.Channel.InstanceID] + "'")[0];
                            channel[Alias.Instance.ID] = row[Alias.Instance.ID];
                            channel[Alias.Instance.InnerID] = row[Alias.Instance.InnerID];
                            channel[Alias.Module.ID] = row[Alias.Instance.ModuleID];
                            channel[Alias.Module.Name] = row[Alias.Module.Name];
                            channel[Alias.Module.Namespace] = row[Alias.Module.Namespace];
                        }

                        if (!Convert.IsDBNull(channel[Alias.Channel.TemplatePath]))
                        {
                            channel[Alias.Channel.Path] = _webPath + channel[Alias.Channel.Path];

                            template = templateBll.Get(channel[Alias.Channel.TemplatePath].ToString());
                            if (template != null)
                                channel[Alias.Template.IsAdmin] = template[Alias.Template.IsAdmin];
                        }
                    }
                }

                _initializedEvents[1].Set();
            }
        }
        private void _UpdateExtraInstances()
        {
            lock (_initializedEvents[1])
            {
                _initializedEvents[1].Reset();

                _extraInstances = null;
                using (SystemModule.Channel.IExtraInstance bll = (SystemModule.Channel.IExtraInstance)_CreateObject("Channel.ExtraInstance", 0, null, false))
                {
                    _extraInstances = bll.List(-1, -1, null, null);
                }

                _initializedEvents[1].Set();
            }
        }
        private void _UpdatePages()
        {
            lock (_initializedEvents[1])
            {
                _initializedEvents[1].Reset();

                using (SystemModule.Template.IPage bll = (SystemModule.Template.IPage)_CreateObject("Template.Page", 0, null, false))
                {
                    _pages = bll.List(-1, -1, (Dictionary<string, object>)null, (Dictionary<string, string>)null);
                }

                _initializedEvents[1].Set();
            }
        }
        private void _UpdateRoles()
        {
            lock (_initializedEvents[1])
            {
                _initializedEvents[1].Reset();

                using (IRole bll = (IRole)_CreateObject("Role", 0, null, false))
                {
                    _roles = bll.List(-1, -1, null, null);
                }

                _initializedEvents[1].Set();
            }
        }
        private void _UpdatePrivileges()
        {
            lock (_initializedEvents[1])
            {
                _initializedEvents[1].Reset();

                using (IPrivilege bll = (IPrivilege)_CreateObject("Privilege", 0, null, false))
                {
                    _privileges = bll.List(-1, -1, null, null);
                }

                _initializedEvents[1].Set();
            }
        }
        private void _UpdatePermissions()
        {
            lock (_initializedEvents[1])
            {
                _initializedEvents[1].Reset();

                using (IPermission bll = (IPermission)_CreateObject("Permission", 0, null, false))
                {
                    _permissions = bll.List(-1, -1, null, null);
                }

                _initializedEvents[1].Set();
            }
        }
        private void UpdateObjects()
        {
            lock (_initializedEvents[1])
            {
                _initializedEvents[1].Reset();

                using (AliCMS.SystemModule.Module.IObject bll = (AliCMS.SystemModule.Module.IObject)_CreateObject("Module.Object", 0, null, false))
                {
                    _objects = bll.List(-1, -1, (Dictionary<string, object>)null, (Dictionary<string, string>)null);
                }

                _objects.Columns.Add("FullName", typeof(string));
                foreach (DataRow row in _objects.Rows)
                {
                    string name = row[Alias.Module.Object.Name].ToString();
                    DataRow parent = row;
                    while (!Guid.Equals(parent[Alias.Module.Object.ParentID], Guid.Empty))
                    {
                        parent = _objects.Select(Alias.Module.Object.ID + "='" + parent[Alias.Module.Object.ParentID] + "'")[0];
                        name = parent[Alias.Module.Object.Name] + "." + name;
                    }
                    row["FullName"] = name;
                }

                _initializedEvents[1].Set();
            }
        }
        private void UpdateProperties()
        {
            lock (_initializedEvents[1])
            {
                _initializedEvents[1].Reset();

                using (AliCMS.SystemModule.Module.Object.IProperty bll = (AliCMS.SystemModule.Module.Object.IProperty)_CreateObject("Module.Object.Property", 0, null, false))
                {
                    _properties = bll.List(-1, -1, (Dictionary<string, object>)null, (Dictionary<string, string>)null);
                }

                _initializedEvents[1].Set();
            }
        }
        private void _UpdateRewrite()
        {
            lock (_initializedEvents[1])
            {
                _initializedEvents[1].Reset();

                using (IRewrite bll = (IRewrite)_CreateObject("Rewrite", 0, String.Empty, false))
                {
                    _rewriteRules = bll.List(-1, -1, null, null);
                }

                _initializedEvents[1].Set();
            }
        }
        private void _UpdateTask()
        {
            lock (_initializedEvents[1])
            {
                _initializedEvents[1].Reset();

                lock (_webAbsolutePath)
                {
                    using (ITask bll = (ITask)_CreateObject("Task", 0, String.Empty, false))
                    {
                        _tasks = bll.List(-1, -1, null, null);
                    }
                }

                _initializedEvents[1].Set();
            }
        }
        private void _UpdateClients()
        {
            lock (_initializedEvents[1])
            {
                _initializedEvents[1].Reset();

                using (IClient bll = (IClient)_CreateObject("Client", 0, null, false))
                {
                    _clients = bll.List(-1, -1, null, null);
                }

                _initializedEvents[1].Set();
            }
        }
		private void _UpdateWhiteList()
        {
            lock (_initializedEvents[1])
            {
                _initializedEvents[1].Reset();

				using (IWhiteList bll = (IWhiteList)_CreateObject("WhiteList", 0, null, false))
                {
					_whiteList = bll.List(-1, -1, null, null);
                }

                _initializedEvents[1].Set();
            }
        }
		private void _UpdateBlackList()
        {
            lock (_initializedEvents[1])
            {
                _initializedEvents[1].Reset();

				using (IBlackList bll = (IBlackList)_CreateObject("BlackList", 0, null, false))
                {
					_blackList = bll.List(-1, -1, null, null);
                }

                _initializedEvents[1].Set();
            }
        }
		private void _UpdateKeywordFilter()
        {
            lock (_initializedEvents[1])
            {
                _initializedEvents[1].Reset();

				using (IKeywordFilter bll = (IKeywordFilter)_CreateObject("KeywordFilter", 0, null, false))
                {
					_keywordFilter = bll.List(-1, -1, null, null);
                }

                _initializedEvents[1].Set();
            }
        }

        private void RunTask()
        {
            while (true)
            {
                WaitHandle.WaitAll(_initializedEvents);
                Thread.Sleep(30000);

                try
                {
                    lock (_webAbsolutePath)
                    {
                        for (int i = _tasks.Rows.Count - 1; i >= 0; i--)
                        {
                            DataRow task = _tasks.Rows[i];
                            DateTime now = DateTime.Now;
                            DateTime start = Convert.ToDateTime(task[Alias.Task.StartTime]);
                            bool run = false;

                            if (start > now)
                                continue;

                            if (task[Alias.Task.Every].ToString() == "Once")
                            {
                                if (now.Year == start.Year && now.Month == start.Month && now.Day == start.Day && now.Hour == start.Hour && now.Minute == start.Minute)
                                    run = true;
                            }
                            else if (task[Alias.Task.Every].ToString() == "Year")
                            {
                                if (now.DayOfYear == start.Day && now.Hour == start.Hour && now.Minute == start.Minute)
                                    run = true;
                            }
                            else if (task[Alias.Task.Every].ToString() == "Season")
                            {
                                if (start.Subtract(now).TotalDays % 90 == 0 && now.Hour == start.Hour && now.Minute == start.Minute)
                                    run = true;
                            }
                            else if (task[Alias.Task.Every].ToString() == "Month")
                            {
                                if (now.Day == start.Day && now.Hour == start.Hour && now.Minute == start.Minute)
                                    run = true;
                            }
                            else if (task[Alias.Task.Every].ToString().IndexOf("Week") == 0)
                            {
                                if (now.Hour == start.Hour && now.Minute == start.Minute)
                                {
                                    string[] days = task[Alias.Task.Every].ToString().Split(',');
                                    foreach (string day in days)
                                    {
                                        DayOfWeek dayOfWeek = DayOfWeek.Monday;
                                        switch (day.Trim())
                                        {
                                            case "0":
                                                dayOfWeek = DayOfWeek.Sunday;
                                                break;
                                            case "1":
                                                dayOfWeek = DayOfWeek.Monday;
                                                break;
                                            case "2":
                                                dayOfWeek = DayOfWeek.Tuesday;
                                                break;
                                            case "3":
                                                dayOfWeek = DayOfWeek.Thursday;
                                                break;
                                            case "4":
                                                dayOfWeek = DayOfWeek.Wednesday;
                                                break;
                                            case "5":
                                                dayOfWeek = DayOfWeek.Friday;
                                                break;
                                            case "6":
                                                dayOfWeek = DayOfWeek.Saturday;
                                                break;
                                        }

                                        if (now.DayOfWeek == dayOfWeek)
                                        {
                                            run = true;
                                            break;
                                        }
                                    }
                                }
                            }
                            else if (task[Alias.Task.Every].ToString() == "Day")
                            {
                                if (now.Hour == start.Hour && now.Minute == start.Minute)
                                    run = true;
                            }
                            else if (task[Alias.Task.Every].ToString() == "Hour")
                            {
                                if (now.Minute == start.Minute)
                                    run = true;
                            }

                            bool remove = task[Alias.Task.Every].ToString() == "Once";

                            #region 运行处理程序
                            if (run)
                            {
                                DataRow[] rows = _instances.Select(Alias.Instance.ID + "='" + task[Alias.Task.InstanceID] + "'");
                                if (rows.Length == 0)
                                {
                                    remove = true;
                                }
                                else
                                {
                                    using (IObject bll = _CreateObject(task[Alias.Task.Object].ToString(), (int)rows[0][Alias.Instance.InnerID], task[Alias.Task.Lang].ToString(), false))
                                    {
                                        if (bll == null)
                                        {
                                            remove = true;
                                        }
                                        else
                                        {
                                            bll.GetType().InvokeMember(task[Alias.Task.Operation].ToString(), BindingFlags.InvokeMethod, null, bll, new object[] { task });
                                        }
                                    }
                                }
                            }
                            #endregion

                            #region 如果是定时，把它移除
                            if (remove)
                            {
                                using (ITask bll = (ITask)_CreateObject("Task", 0, String.Empty, false))
                                {
                                    bll.Delete((int)_tasks.Rows[i][Alias.Task.InnerID]);
                                }

                                _tasks.Rows.RemoveAt(i);
                            }
                            #endregion
                        }
                    }
                }
                catch (Exception exp)
                {
                    using (SystemModule.ILog logBll = (SystemModule.ILog)_CreateObject("Log", 0, "CHS", false))
                    {
                        Dictionary<string, object> item = new Dictionary<string, object>();
                        item[SystemModule.Alias.Log.Message] = exp.ToString();
                        item[SystemModule.Alias.Log.CreateTime] = DateTime.Now;
                        logBll.Add(item);
                    }
                }
            }
        }

        private delegate StringBuilder SubBuildCallback(IEnumerator data, ref object current, ref int index);
        private string BuildTreeControl(TreeNode item, XmlNode control)
        {
            string result = "";

            string space = control.SelectSingleNode("Space").InnerText;
            string middleOpen = control.SelectSingleNode("MiddleOpen").InnerText;
            string middle = control.SelectSingleNode("Middle").InnerText;
            string bottomOpen = control.SelectSingleNode("BottomOpen").InnerText;
            string bottom = control.SelectSingleNode("Bottom").InnerText;
            string topOpen = control.SelectSingleNode("TopOpen").InnerText;
            string top = control.SelectSingleNode("Top").InnerText;
            string line = control.SelectSingleNode("Line").InnerText;

            TreeNode parent = item.Parent;

            if (parent != null)
            {
                result = space;
                if (item.Next != null)
                {
                    if (item.ChildNodes.Count > 0)
                        result += middleOpen;
                    else
                        result += middle;
                }
                else
                {
                    if (item.ChildNodes.Count > 0)
                        result += bottomOpen;
                    else
                        result += bottom;
                }
            }
            else
            {
                if (item.Next != null)
                {
                    if (item.Prev == null)
                    {
                        if (item.ChildNodes.Count > 0)
                            result = topOpen;
                        else
                            result = top;
                    }
                    else
                    {
                        if (item.ChildNodes.Count > 0)
                            result = middleOpen;
                        else
                            result = middle;
                    }
                }
                else
                {
                    if (item.Prev == null)
                    {
                        if (item.ChildNodes.Count > 0)
                            result = topOpen;
                        else
                            result = top;
                    }
                    else
                    {
                        if (item.ChildNodes.Count > 0)
                            result = bottomOpen;
                        else
                            result = bottom;
                    }
                }
            }

            while (parent != null)
            {
                if (parent.Parent != null)
                {
                    if (parent.Next != null)
                        result = space + line + result;
                    else
                        result = space + space + result;
                }
                else
                {
                    if (parent.Next != null)
                        result = line + result;
                    else
                        result = space + result;
                }

                parent = parent.Parent;
            }

            return result;
        }
        private List<TreeNode> MakeTreeNodeList(DataTable data, TreeNode parent, string rootID, string parentIDField, string idField)
        {
            List<TreeNode> result = new List<TreeNode>();

            DataRow[] rows = null;
            if (parent == null)
            {
                if (String.IsNullOrEmpty(rootID))
                    rows = data.Select(parentIDField + " IS NULL");
                else
                    rows = data.Select(parentIDField + "='" + rootID + "'");
            }
            else
            {
                rows = data.Select(parentIDField + "='" + parent.Row[idField] + "'");
            }

            TreeNode prev = null;
            for (int i = 0; i < rows.Length; i++)
            {
                TreeNode node = new TreeNode();
                node.Row = rows[i];
                node.Parent = parent;
                node.Prev = prev;

                result.Add(node);
                node.ChildNodes = MakeTreeNodeList(data, node, rootID, parentIDField, idField);

                if (prev != null)
                    prev.Next = node;
                prev = node;
            }

            return result;
        }
        /*
        private StringBuilder _BuildTree(XmlNode template, DataTable data, Dictionary<string, object> replacementPair, string parentIDField, string idField)
        {
            object current = null;
            int index = 0;
            return _BuildTree(template, data, replacementPair, parentIDField, idField, ref current, ref index);
        }
        private StringBuilder _BuildTree(XmlNode template, DataTable data, Dictionary<string, object> replacementPair, string parentIDField, string idField, ref object current, ref int index)
        {
            List<TreeNode> itemList = MakeTreeNodeList(data, null, Guid.Empty.ToString(), parentIDField, idField);
            return Build(template, itemList.GetEnumerator(), replacementPair, ref current, ref index);
        }
        */
        private StringBuilder _BuildTree(XmlNode template, DataTable data, Dictionary<string, object> replacementPair, string rootID, string parentIDField, string idField, ref object current, ref int index)
        {
            List<TreeNode> itemList = MakeTreeNodeList(data, null, rootID, parentIDField, idField);
            return Build(template, itemList.GetEnumerator(), replacementPair, ref current, ref index);
        }
        private StringBuilder _Build(XmlNode template, DataTable data, Dictionary<string, object> replacementPair)
        {
            object current = null;
            int index = 0;
            return Build(template, (IEnumerator<DataRow>)data.Rows.GetEnumerator(), replacementPair, ref current, ref index);
        }
        private StringBuilder _Build(XmlNode template, DataTable data, Dictionary<string, object> replacementPair, ref object current, ref int index)
        {
            return Build(template, (IEnumerator<DataRow>)data.Rows.GetEnumerator(), replacementPair, ref current, ref index);
        }
        private StringBuilder _Build(XmlNode template, DataRow[] data, Dictionary<string, object> replacementPair)
        {
            object current = null;
            int index = 0;
            return Build(template, data.GetEnumerator(), replacementPair, ref current, ref index);
        }
        private StringBuilder Build(XmlNode template, IEnumerator data, Dictionary<string, object> replacementPair, ref object current, ref int index)
        {
            string recordSeparator = template.SelectSingleNode("RecordSeparator").InnerText;
            string cellSeparator = template.SelectSingleNode("ColSeparator").InnerText;
            string rowSeparator = template.SelectSingleNode("RowSeparator").InnerText;

            string recordTpl = template.SelectSingleNode("Record").InnerText;
            string cellTpl = template.SelectSingleNode("Col").InnerText;
            string rowTpl = template.SelectSingleNode("Row").InnerText;
            string allTpl = template.SelectSingleNode("All").InnerText;

            int maxRecord = Convert.ToInt32(template.SelectSingleNode("Col").Attributes["MaxRecord"].Value);
            int maxCol = Convert.ToInt32(template.SelectSingleNode("Row").Attributes["MaxCol"].Value);

            SubBuildCallback SubBuild = null;
            SubBuild = delegate(IEnumerator subData, ref object subCurrent, ref int subIndex)
            {
                StringBuilder record = new StringBuilder();
                StringBuilder cell = new StringBuilder();
                StringBuilder row = new StringBuilder();
                StringBuilder all = new StringBuilder();

                subIndex = -1;
                while (subData.MoveNext())
                {
                    subIndex++;
                    subCurrent = subData.Current;

                    record.Remove(0, record.Length);
                    if (cell.Length != 0)
                        record.Append(recordSeparator);
                    record.Append(recordTpl);
                    foreach (KeyValuePair<string, object> kvp in replacementPair)
                    {
                        if (kvp.Key == "{#Index#}")
                        {
                            record.Replace(kvp.Key, subIndex.ToString());
                        }
                        else if (subCurrent is TreeNode)
                        {
                            if (kvp.Value is CustomBuildTree)
                            {
                                record.Replace(kvp.Key, ((CustomBuildTree)kvp.Value)((TreeNode)subCurrent, subIndex));
                            }
                            else if (kvp.Key == "{#Control#}")
                            {
                                record.Replace(kvp.Key, BuildTreeControl((TreeNode)subCurrent, template.SelectSingleNode("Control")));
                            }
                            else if (kvp.Key == "{#ChildRecords#}")
                            {
                                record.Replace(kvp.Key, SubBuild(((TreeNode)subCurrent).ChildNodes.GetEnumerator(), ref subCurrent, ref subIndex).ToString());
                                subCurrent = subData.Current;
                            }
                            else if (kvp.Key == "{#ParentID#}")
                            {
                                record.Replace(kvp.Key, ((TreeNode)subCurrent).Parent == null ? "" : ((TreeNode)subCurrent).Parent.Row[Convert.ToString(kvp.Value)].ToString());
                            }
                            else if (kvp.Key == "{#ParentInnerID#}")
                            {
                                record.Replace(kvp.Key, ((TreeNode)subCurrent).Parent == null ? "" : ((TreeNode)subCurrent).Parent.Row[Convert.ToString(kvp.Value)].ToString());
                            }
                            else if (kvp.Value is int)
                            {
                                record.Replace(kvp.Key, ((TreeNode)subCurrent).Row[(int)kvp.Value].ToString());
                            }
                            else
                            {
                                record.Replace(kvp.Key, ((TreeNode)subCurrent).Row[Convert.ToString(kvp.Value)].ToString());
                            }
                        }
                        else
                        {
                            if (kvp.Value is CustomBuild)
                                record.Replace(kvp.Key, ((CustomBuild)kvp.Value)((DataRow)subCurrent, subIndex));
                            else if (kvp.Value is int)
                                record.Replace(kvp.Key, ((DataRow)subCurrent)[(int)kvp.Value].ToString());
                            else
                                record.Replace(kvp.Key, ((DataRow)subCurrent)[Convert.ToString(kvp.Value)].ToString());
                        }
                    }

                    if (cell.Length == 0)
                        cell.Append(cellTpl);
                    cell.Replace("{#Record#}", record.ToString());

                    if (maxRecord != 0 && (subIndex + 1) % maxRecord == 0)
                    {
                        cell.Replace("{#Record#}", "");
                        if (row.Length == 0)
                        {
                            row.Append(rowTpl);
                            row.Replace("{#Col#}", cell.ToString());
                        }
                        else
                        {
                            row.Replace("{#Col#}", cellSeparator + cell.ToString());
                        }
                        cell.Remove(0, cell.Length);

                        if (maxCol != 0 && ((subIndex + 1) / maxRecord) % maxCol == 0)
                        {
                            row.Replace("{#Col#}", "");
                            if (all.Length == 0)
                            {
                                all.Append(allTpl);
                                all.Replace("{#Row#}", row.ToString());
                            }
                            else
                            {
                                all.Replace("{#Row#}", rowSeparator + row.ToString());
                            }
                            row.Remove(0, row.Length);
                        }
                    }
                }

                if (cell.Length > 0)
                {
                    cell.Replace("{#Record#}", "");
                    if (row.Length == 0) row.Append(rowTpl);
                    if (maxRecord != 0 && subIndex / maxRecord > 1)
                        row.Replace("{#Col#}", cellSeparator + cell.ToString());
                    else
                        row.Replace("{#Col#}", cell.ToString());
                }

                if (row.Length > 0)
                {
                    row.Replace("{#Col#}", "");
                    if (all.Length == 0) all.Append(allTpl);
                    if (maxRecord != 0 && maxCol != 0 && (subIndex / maxRecord) / maxCol > 1)
                        all.Replace("{#Row#}", rowSeparator + row.ToString());
                    else
                        all.Replace("{#Row#}", row.ToString());
                }

                all.Replace("{#Row#}", "");

                return all;
            };

            return SubBuild(data, ref current, ref index);
        }

        private DataTable _SplitToTable(params object[] list)
        {
            DataTable result = new DataTable();

            foreach (object param in list)
            {
                result.Columns.Add();

                string temp = param.ToString();
                if (temp.StartsWith("|"))
                    temp = temp.Substring(1);
                if (temp.EndsWith("|"))
                    temp = temp.Substring(0, temp.Length - 1);

                if (temp == "")
                    continue;

                string[] values = temp.Split(new char[] { '|' });
                for (int i = 0; i < values.Length; i++)
                {
                    DataRow row = null;
                    if (i < result.Rows.Count)
                    {
                        row = result.Rows[i];
                    }
                    else
                    {
                        row = result.NewRow();
                        result.Rows.Add(row);
                    }

                    row[result.Columns.Count - 1] = values[i];
                }
            }

            return result;
        }
        private Dictionary<string, object> _ParseContent(object allContent, AliCMS.Page page)
        {
            string title = String.Empty;
            string titles = String.Empty;
            StringBuilder content = new StringBuilder();
            StringBuilder script = new StringBuilder();
            List<int> ids = new List<int>();

            Match match = null;
            MatchCollection matches = Regex.Matches(allContent.ToString(), @"<div style=""page-break-after:\s?always"">.*?</div>");
            if (matches.Count > 0)
            {
                int start = 0;
                for (int j = 0; j <= matches.Count; j++)
                {
                    #region 获取副标题

                    match = Regex.Match(allContent.ToString().Substring(start, j == matches.Count ? allContent.ToString().Length - start : matches[j].Index - start), @"\[title\](?<v>(.|\s)*?)\[/title\]");
                    titles += "|";
                    if (match.Success)
                        titles += match.Groups["v"].Value;
                    #endregion

                    if (j + 1 == page.PageNumber)
                    {
                        content.Append(allContent.ToString().Substring(start, j == matches.Count ? allContent.ToString().Length - start : matches[j].Index - start));

                        #region 播放器

                        MatchCollection players = Regex.Matches(content.ToString(), @"\[player(=(?<g>\{?[a-fA-F0-9]{8}(-?[a-fA-F0-9]{4}){3}-?[a-fA-F0-9]{12}\}?))?\](?<v>(.|\s)*?)\[/player\]");
                        if (players.Count > 0)
                        {
                            using (IPlayer bll = (IPlayer)_CreateObject("Player", 0, page.PageLang, false))
                            {
                                for (int i = players.Count - 1; i >= 0; i--)
                                {
                                    content.Remove(players[i].Index, players[i].Length);
                                    int id = -1;

                                    if (players[i].Groups["g"].Success)
                                    {
                                        Dictionary<string, object> item = bll.Get(new Guid(players[i].Groups["g"].Value));
                                        if (item != null)
                                        {
                                            id = (int)item[Alias.Player.InnerID];
                                            if (!ids.Contains(id))
                                            {
                                                script.Append("<script type=\"text/javascript\" src=\"" + item[Alias.Player.CodeFile] + "\"></script>");
                                                ids.Add(id);
                                            }
                                        }
                                        else
                                        {
                                            content.Insert(players[i].Index, players[i].Groups["v"].Value);
                                        }
                                    }

                                    if (id != -1)
                                        content.Insert(players[i].Index, "<script type=\"text/javascript\">Player" + id + ".WritePlayer(\"" + players[i].Groups["v"].Value + "\");</script>");
                                }
                            }
                        }
                        #endregion
                    }

                    if (j < matches.Count)
                        start = matches[j].Index + matches[j].Length;
                }
            }
            else
            {
                content.Append(allContent);

                #region 获取副标题

                match = Regex.Match(content.ToString(), @"\[title\](?<v>(.|\s)*?)\[/title\]");
                if (match.Success)
                {
                    title = match.Groups["v"].Value;

                    content.Remove(match.Index, match.Length);
                    content.Insert(match.Index, title);
                }
                #endregion

                #region 播放器

                MatchCollection players = Regex.Matches(content.ToString(), @"\[player(=(?<g>\{?[a-fA-F0-9]{8}(-?[a-fA-F0-9]{4}){3}-?[a-fA-F0-9]{12}\}?))?\](?<v>(.|\s)*?)\[/player\]");
                if (players.Count > 0)
                {
                    using (IPlayer bll = (IPlayer)_CreateObject("Player", 0, page.PageLang, false))
                    {
                        for (int i = players.Count - 1; i >= 0; i--)
                        {
                            content.Remove(players[i].Index, players[i].Length);
                            int id = -1;

                            if (players[i].Groups["g"].Success)
                            {
                                Dictionary<string, object> item = bll.Get(new Guid(players[i].Groups["g"].Value));
                                if (item != null)
                                {
                                    id = (int)item[Alias.Player.InnerID];
                                    if (!ids.Contains(id))
                                    {
                                        script.Append("<script type=\"text/javascript\" src=\"" + item[Alias.Player.CodeFile] + "\"></script>");
                                        ids.Add(id);
                                    }
                                }
                                else
                                {
                                    content.Insert(players[i].Index, players[i].Groups["v"].Value);
                                }
                            }

                            if (id != -1)
                                content.Insert(players[i].Index, "<script type=\"text/javascript\">Player" + id + ".WritePlayer(\"" + players[i].Groups["v"].Value + "\");</script>");
                        }
                    }
                }
                #endregion
            }

            if (script.Length > 0)
            {
                script.Insert(0, "<script type=\"text/javascript\" src=\"" + _GetCommonPath() + "swfobject.js\"></script>");
                content.Insert(0, script);
            }

            titles = titles.Trim('|');
            if (String.IsNullOrEmpty(titles))
                titles = null;

            Dictionary<string, object> result = new Dictionary<string, object>();
            result["Title"] = title;
            result["Titles"] = titles;
            result["PageCount"] = matches.Count + 1;
            result["Content"] = content.ToString();
            return result;
        }

        private DataTable _GetPath(DataRow channel)
        {
            DataTable result = new DataTable();
            result.Columns.Add("Name", typeof(string));
            result.Columns.Add("URL", typeof(string));

            string url = channel[Alias.Channel.Path].ToString();

            DataRow newRow = result.NewRow();
            newRow["Name"] = channel[Alias.Instance.Name + channel[Alias.Channel.Lang]];
            newRow["URL"] = url;
            result.Rows.Add(newRow);

            DataRow[] rows = _channels.Select(Alias.Channel.ID + " IS NOT NULL", Alias.Channel.Path + " DESC");
            foreach (DataRow row in rows)
            {
                if (Guid.Equals(row[Alias.Channel.ID], channel[Alias.Channel.ID]))
                     continue;

                 if (url.StartsWith(row[Alias.Channel.Path].ToString()))
                {
                    url = row[Alias.Channel.Path].ToString();

                    newRow = result.NewRow();
                    newRow["Name"] = row[Alias.Instance.Name + channel[Alias.Channel.Lang]];
                    newRow["URL"] = url;
                    result.Rows.InsertAt(newRow, 0);
                }
            }

            return result;
        }
        private DataTable _GetPath(string url, string lang)
        {
            DataTable result = new DataTable();
            result.Columns.Add("Name", typeof(string));
            result.Columns.Add("URL", typeof(string));

            DataRow[] rows = _channels.Select(Alias.Channel.ID + " IS NOT NULL", Alias.Channel.Path + " DESC");
            foreach (DataRow row in rows)
            {
                string path = row[Alias.Channel.Path].ToString();

                if (url != path && url.StartsWith(path))
                {
                    url = path;

                    DataRow newRow = result.NewRow();
                    newRow["Name"] = row[Alias.Instance.Name + lang];
                    newRow["URL"] = url;
                    result.Rows.InsertAt(newRow, 0);
                }
            }

            return result;
        }
        private string _GetPathInChannel(DataRow channel, string pathInTemplate, string templatePath)
        {
            if (!pathInTemplate.EndsWith(".dwt.aspx"))
                pathInTemplate += ".dwt.aspx";

            if (channel.Table.Columns.Contains(Alias.Module.Namespace) && channel[Alias.Module.Namespace].ToString() == "AliCMS.SystemModule")
            {
                return pathInTemplate.Replace(".dwt.aspx", ".aspx").Replace('\\', '/');
            }
            else
            {
                if (String.IsNullOrEmpty(templatePath))
                    templatePath = channel[Alias.Channel.TemplatePath].ToString();

                DataRow[] rows = _pages.Select(Alias.Template.Page.TemplatePath + "='" + templatePath + "' AND " + Alias.Template.Page.PathInTemplate + " LIKE '" + pathInTemplate + "*'");
                if (rows.Length == 0)
                {
                    #region 检查是否实际已经存在了，但是缓存还没有更新。如果是更新缓存
                    string path = _GetTemplatePath(templatePath) + pathInTemplate;
                    if (File.Exists(_MapPath(path)))
                    {
                        _UpdatePages();
                        rows = _pages.Select(Alias.Template.Page.TemplatePath + "='" + templatePath + "' AND " + Alias.Template.Page.PathInTemplate + "='" + pathInTemplate + "'");
                        if (rows.Length > 0)
                            return rows[0][Alias.Template.Page.PathInChannel].ToString();
                    }
                    #endregion

                    return String.Empty;
                }
                else
                {
                    string pathInChannel = rows[0][Alias.Template.Page.PathInChannel].ToString();
                    if (pathInChannel == String.Empty)
                        pathInChannel = pathInTemplate.Replace(".dwt.aspx", ".aspx").Replace('\\', '/');
                    return pathInChannel;
                }
            }
        }

        private bool _HasRight(string userID, int instanceInnerID, Guid? subInstanceID, string lang, object objectIDOrName, object operationIDOrName, Guid recordID)
        {
            DataRow[] rows = _instances.Select(Alias.Instance.InnerID + "=" + instanceInnerID);
            if (rows.Length == 0)
                throw new Exception("lang_no_right");

            Dictionary<string, string> user = null;
            if (userID == null)
                user = _User;

            if (!String.IsNullOrEmpty(userID) || (user != null && user["Namespace"] == "AliCMS.UserModule") || subInstanceID != null)//用户
            {
                rows = _instances.Select(Alias.Module.Namespace + "='AliCMS.UserModule'");
                if (rows.Length > 0)
                {
                    using (IObject bll = _CreateObject("InstanceConfig", (int)rows[0][Alias.Instance.InnerID], lang, false))
                    {
                        return (bool)bll.GetType().InvokeMember("HasRight", BindingFlags.InvokeMethod, null, bll, new object[] { userID == null ? (object)user : (object)userID, instanceInnerID, subInstanceID, lang, objectIDOrName, operationIDOrName, recordID });
                    }
                }
            }
            else//管理员
            {
                Guid instanceID = (Guid)rows[0][Alias.Instance.ID];

                Guid roleID = Guid.Empty;
                if (userID == null)
                {
					if (user != null && user["RoleID"] != "")
                        roleID = new Guid(user["RoleID"]);
                }
                else if (userID != "")
                {
					//TODO
                }

				return HasRightByRole(roleID, instanceID, lang, objectIDOrName, operationIDOrName, recordID);
            }

            return false;
        }

		//判断管理员角色是否有权限
		internal bool HasRightByRole(Guid? roleID, Guid instanceID, string lang, object objectIDOrName, object operationIDOrName, Guid recordID)
		{
			string condition = String.Empty;

			string condition1 = Alias.Permission.InstanceID + "='" + instanceID + "'";
			condition1 += " AND " + Alias.Permission.Lang + "='" + lang + "'";
			if (objectIDOrName is Guid)
				condition1 += " AND " + Alias.Permission.ObjectID + "='" + objectIDOrName + "'";
			else
				condition1 += " AND " + Alias.Permission.ObjectName + "='" + objectIDOrName + "'";
			if (operationIDOrName is Guid)
				condition1 += " AND " + Alias.Permission.OperationID + "='" + operationIDOrName + "'";
			else
				condition1 += " AND " + Alias.Permission.OperationName + "='" + operationIDOrName + "'";
			if (recordID != Guid.Empty)
				condition1 += " AND " + Alias.Permission.RecordID + "='" + recordID + "'";

			string condition2 = " AND " + Alias.Privilege.InstanceID + "='" + instanceID + "'";
			condition2 += " AND " + Alias.Privilege.Lang + "='" + lang + "'";
			if (objectIDOrName is Guid)
				condition2 += " AND " + Alias.Privilege.ObjectID + "='" + objectIDOrName + "'";
			else
				condition2 += " AND " + Alias.Privilege.ObjectName + "='" + objectIDOrName + "'";
			if (operationIDOrName is Guid)
				condition2 += " AND " + Alias.Privilege.OperationID + "='" + operationIDOrName + "'";
			else
				condition2 += " AND " + Alias.Privilege.OperationName + "='" + operationIDOrName + "'";

			if (roleID == null || roleID == Guid.Empty)
			{
				#region 判断权限
				condition = condition1 + " AND " + Alias.Permission.RoleID + "='" + Guid.Empty + "'";
				DataRow[] rows = _permissions.Select(condition);
				foreach (DataRow row in rows)
				{
					if ((bool)row[Alias.Permission.IsAllowed])
						return true;
				}
				#endregion

				#region 判断特权
				condition = Alias.Privilege.RoleID + "='" + Guid.Empty + "'" + condition2;
				rows = _privileges.Select(condition);
				if (rows.Length != 0)
					return true;
				#endregion
			}
			else
			{
				#region 判断权限
				condition = condition1 + " AND " + Alias.Permission.RoleID + "='" + roleID + "'";
				DataRow[] rows = _permissions.Select(condition);
				foreach (DataRow row in rows)
				{
					if ((bool)row[Alias.Permission.IsAllowed])
						return true;
				}
				#endregion

				#region 判断特权
				bool hasPrivilegeDefault = (bool)_roles.Select(Alias.Role.ID + "='" + roleID + "'")[0][Alias.Role.HasPrivilegeByDefault];
				condition = Alias.Privilege.RoleID + "='" + roleID + "'" + condition2;
				rows = _privileges.Select(condition);
				if (rows.Length != 0)
				{
					if (!hasPrivilegeDefault)
						return true;
				}
				else
				{
					if (hasPrivilegeDefault)
						return true;
				}
				#endregion
			}

			return false;
		}

        private string _GetConnectionString(int instanceID, string db)
        {
            string connectionString = String.Empty;
            if (instanceID == 0 || _instances == null)
            {
                connectionString = _site[Alias.InstanceConfig.ConnectionString];
            }
            else
            {
                DataRow[] rows = _instances.Select(Alias.Instance.InnerID + "=" + instanceID);
                if (rows.Length == 0)
                    return connectionString;

                if (String.IsNullOrEmpty(rows[0][Alias.Instance.ConnectionString].ToString()))
                    connectionString = _site[Alias.InstanceConfig.ConnectionString];
                else
                    connectionString = rows[0][Alias.Instance.ConnectionString].ToString();
            }

            if (String.IsNullOrEmpty(db))
                return connectionString;

            string[] pairs = connectionString.Split(';');
            connectionString = String.Empty;
            bool find = false;
            foreach (string pair in pairs)
            {
                string[] p = pair.Split('=');
                if (!find)
                {
                    if (p[0].Equals("Initial Catalog", StringComparison.InvariantCultureIgnoreCase) ||
                        p[0].Equals("Database", StringComparison.InvariantCultureIgnoreCase) ||
                        p[0].Equals("Data Source", StringComparison.InvariantCultureIgnoreCase))
                    {
                        p[1] = db;
                        find = true;
                    }
                }

                if (connectionString != String.Empty)
                    connectionString += ";";

                connectionString += p[0] + "=" + p[1];
            }

            return connectionString;
        }

        private string _MakeValidateKey()
        {
            HttpContext.Current.Session["ValidateKey"] = HttpContext.Current.Session.SessionID;

            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);

            StreamWriter sw = new StreamWriter(cs, Encoding.UTF8);
            sw.Write(HttpContext.Current.Session.SessionID);
            sw.Close();

            string result = Convert.ToBase64String(des.Key).Replace('=', '$') + '!' + Convert.ToBase64String(des.IV).Replace('=', '@') + '#' + Convert.ToBase64String(ms.ToArray());

            cs.Close();
            ms.Close();
            des.Clear();

            return result;
        }
        internal bool IsCorrectValidateKey(string src)
        {
            string result = String.Empty;

            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();

                int start = src.IndexOf('!');
                byte[] key = Convert.FromBase64String(src.Substring(0, start).Replace('$', '='));

                int end = src.IndexOf('#', start + 1);
                byte[] iv = Convert.FromBase64String(src.Substring(start + 1, end - start - 1).Replace('@', '='));

                MemoryStream ms = new MemoryStream(Convert.FromBase64String(src.Substring(end + 1)));
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, iv), CryptoStreamMode.Read);
                StreamReader sr = new StreamReader(cs, Encoding.UTF8);

                result = sr.ReadToEnd();

                sr.Close();
                cs.Close();
                ms.Close();
                des.Clear();
            }
            catch
            {
            }

            return HttpContext.Current.Session.SessionID.Equals(result);
        }

        private void _EncodeGZipToFile(string text, string path)
        {
            FileStream destination = File.Create(path);
            byte[] buffer = Encoding.UTF8.GetBytes(text);
			DeflateStream zip = null;
            try
            {
				zip = new DeflateStream(destination, CompressionMode.Compress);
                zip.Write(buffer, 0, buffer.Length);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (zip != null)
                    zip.Close();
                destination.Close();
            }
        }
        private string _DecodeGZipToText(string path)
        {
            FileStream source = File.OpenRead(path);
            MemoryStream destination = new MemoryStream();
			DeflateStream unzip = null;
            byte[] buffer = new byte[source.Length];
            try
            {
				unzip = new DeflateStream(source, CompressionMode.Decompress, true);
                while (true)
                {
                    int count = unzip.Read(buffer, 0, (int)source.Length);
                    if (count != 0)
                        destination.Write(buffer, 0, count);

                    if (count != source.Length)
                        break;
                }

                buffer = new byte[destination.Length];
                destination.Read(buffer, 0, (int)destination.Length);
            }
            catch
            {
                throw;
            }
            finally
            {
                if (unzip != null)
                    unzip.Close();
                source.Close();
                destination.Close();
            }

            return Encoding.UTF8.GetString(buffer);
        }

        private void _AddWaterMark(string path, string lang)
        {
            _AddWaterMark(path, lang, null);
        }
        private void _AddWaterMark(string path, string lang, Dictionary<string, string> config)
        {
            if (config == null)
            {
                using (IInstanceConfig bll = (IInstanceConfig)_CreateObject("InstanceConfig", 0, lang, false))
                {
                    config = bll.GetWaterMarkSettings();
                }
            }

            Image input = Image.FromFile(path);
            int r, g, b;
            SizeF size;
            int marginX = Convert.ToInt32(config[Alias.InstanceConfig.WMSMarginX]);
            int marginY = Convert.ToInt32(config[Alias.InstanceConfig.WMSMarginY]);

            #region 首先先判断该图片是否是 gif动画，如果为gif动画不对图片进行改动
            foreach (Guid guid in input.FrameDimensionsList)
            {
                FrameDimension dimension = new FrameDimension(guid);
                if (input.GetFrameCount(dimension) > 1)
                    return;
            }
            #endregion

            #region 文字水印
            if (config[Alias.InstanceConfig.WMSType] == "Text")
            {
                #region 如果没有文字，则直接返回
                if (String.IsNullOrEmpty(config[Alias.InstanceConfig.WMSText]))
                    return;
                #endregion

                #region 准备作图区




                Bitmap bitmap = new Bitmap(input.Width, input.Height, PixelFormat.Format24bppRgb);
                bitmap.SetResolution(input.HorizontalResolution, input.VerticalResolution);
                Graphics graphic = Graphics.FromImage(bitmap);
                graphic.SmoothingMode = SmoothingMode.AntiAlias;
                graphic.DrawImage(input, new Rectangle(0, 0, input.Width, input.Height), 0, 0, input.Width, input.Height, GraphicsUnit.Pixel);
                #endregion

                #region 准备字体和计算文字尺寸（文本不能超过图片大小）




                Font font = null;
                string fontFamily = config[Alias.InstanceConfig.WMSFontFamily];
                int fontSize = Convert.ToInt32(config[Alias.InstanceConfig.WMSFontSize]);
                FontStyle fontStyle = FontStyle.Regular;
                if (Convert.ToBoolean(config[Alias.InstanceConfig.WMSBold]))
                    fontStyle |= FontStyle.Bold;
                if (Convert.ToBoolean(config[Alias.InstanceConfig.WMSItalic]))
                    fontStyle |= FontStyle.Italic;
                do
                {
                    font = new Font(fontFamily, fontSize, fontStyle);
                    size = graphic.MeasureString(config[Alias.InstanceConfig.WMSText], font);
                    if (fontSize == 0 || (size.Width + marginX) < input.Width)
                        break;

                    fontSize--;
                }
                while (true);
                #endregion

                #region 计算位置
                float x = 0, y = 0;
                switch (config[Alias.InstanceConfig.WMSPosition])
                {
                    case "TopLeft":
                        x = marginX;
                        y = marginY;
                        break;
                    case "TopRight":
                        x = input.Width - (size.Width + marginX);
                        y = marginY;
                        break;
                    case "MiddleCenter":
                        x = (input.Width - size.Width) / 2;
                        y = (input.Height - size.Height) / 2;
                        break;
                    case "BottomLeft":
                        x = marginX;
                        y = input.Height - (size.Height + marginY);
                        break;
                    case "BottomRight":
                        x = input.Width - (size.Width + marginX);
                        y = input.Height - (size.Height + marginY);
                        break;
                }
                #endregion

                #region 画背景




                if (!String.IsNullOrEmpty(config[Alias.InstanceConfig.WMSBackColor]))
                {
                    r = Convert.ToInt32(config[Alias.InstanceConfig.WMSBackColor].Substring(1, 2), 16);
                    g = Convert.ToInt32(config[Alias.InstanceConfig.WMSBackColor].Substring(3, 2), 16);
                    b = Convert.ToInt32(config[Alias.InstanceConfig.WMSBackColor].Substring(5, 2), 16);
                    Brush backBrush = new SolidBrush(Color.FromArgb(r, g, b));
                    graphic.FillRectangle(backBrush, x, y, size.Width, size.Height);
                }
                #endregion

                #region 画文字




                int transparency = (int)(Convert.ToDouble(config[Alias.InstanceConfig.WMSTransparency]) * 255);
                r = Convert.ToInt32(config[Alias.InstanceConfig.WMSForeColor].Substring(1, 2), 16);
                g = Convert.ToInt32(config[Alias.InstanceConfig.WMSForeColor].Substring(3, 2), 16);
                b = Convert.ToInt32(config[Alias.InstanceConfig.WMSForeColor].Substring(5, 2), 16);
                Brush brush = new SolidBrush(Color.FromArgb(transparency, r, g, b));
                graphic.DrawString(config[Alias.InstanceConfig.WMSText], font, brush, x, y);
                #endregion

                #region 输出
                ImageFormat format = input.RawFormat;
                input.Dispose();
                bitmap.Save(path, format);
                #endregion

                #region 清理
                brush.Dispose();
                font.Dispose();
                graphic.Dispose();
                bitmap.Dispose();
                #endregion
            }
            #endregion
            #region 图片水印
            else if (config[Alias.InstanceConfig.WMSType] == "Image")
            {
                if (String.IsNullOrEmpty(config[Alias.InstanceConfig.WMSImage]))
                    return;

                #region 读入水印图片
                Image image = Image.FromFile(_MapPath(config[Alias.InstanceConfig.WMSImage]));
                #endregion

                #region 准备作图区




                float[][] ptsArray ={ 
                                        new float[] {1, 0, 0, 0, 0},
                                        new float[] {0, 1, 0, 0, 0},
                                        new float[] {0, 0, 1, 0, 0},
                                        new float[] {0, 0, 0, (float)Convert.ToDouble(config[Alias.InstanceConfig.WMSTransparency]), 0},
                                        new float[] {0, 0, 0, 0, 1}};
                ColorMatrix colorMatrix = new ColorMatrix(ptsArray);
                ImageAttributes imageAttributes = new ImageAttributes();
                imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Default);
                Bitmap bitmap = new Bitmap(input.Width, input.Height, PixelFormat.Format24bppRgb);
                bitmap.SetResolution(input.HorizontalResolution, input.VerticalResolution);
                Graphics graphic = Graphics.FromImage(bitmap);
                graphic.SmoothingMode = SmoothingMode.AntiAlias;
                graphic.DrawImage(input, new Rectangle(0, 0, input.Width, input.Height), 0, 0, input.Width, input.Height, GraphicsUnit.Pixel);
                #endregion

                #region 计算尺寸
                size = new SizeF(image.Width, image.Height);
                if (size.Width + marginX > input.Width)
                    size.Width = input.Width - marginX;
                #endregion

                #region 计算位置
                float x = 0, y = 0;
                switch (config[Alias.InstanceConfig.WMSPosition])
                {
                    case "TopLeft":
                        x = marginX;
                        y = marginY;
                        break;
                    case "TopRight":
                        x = input.Width - (size.Width + marginX);
                        y = marginY;
                        break;
                    case "MiddleCenter":
                        x = (input.Width - size.Width) / 2;
                        y = (input.Height - size.Height) / 2;
                        break;
                    case "BottomLeft":
                        x = marginX;
                        y = input.Height - (size.Height + marginY);
                        break;
                    case "BottomRight":
                        x = input.Width - (size.Width + marginX);
                        y = input.Height - (size.Height + marginY);
                        break;
                }
                #endregion

                #region 画水印图片




                graphic.DrawImage(image, new Rectangle((int)x, (int)y, (int)size.Width, (int)size.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes);
                #endregion

                #region 输出
                ImageFormat format = input.RawFormat;
                input.Dispose();
                bitmap.Save(path, format);
                #endregion

                #region 清理
                graphic.Dispose();
                bitmap.Dispose();
                #endregion
            }
            #endregion
        }

		//作用：将Dictionary<string, object>，Dictionary<string, string>，DataTable，List<Dictionary<string, object>>转换成Json字符串
		private string _BuildJson(object result)
		{
			if (result is Dictionary<string, object>)
			{
				StringBuilder content = new StringBuilder();

				content.Append("{");
				foreach (KeyValuePair<string, object> kvp in (result as Dictionary<string, object>))
				{
					if (content.Length > 1)
						content.Append(",");

					int oledbType = Convert.ToInt32(System.Data.OleDb.OleDbType.VarWChar);
					DataRow[] props = _properties.Select(Alias.Module.Object.Property.UniqueInnerID + "='" + kvp.Key + "'");
					if (props.Length > 0)
						oledbType = Convert.ToInt32(props[0][Alias.Module.Object.Property.OleDbType]);
					string value = "";
					if ((oledbType == 3 || oledbType == 6 || oledbType == 131) && kvp.Value.ToString() != "")//int, moeny, decimal
						value = kvp.Value.ToString();
					else if (oledbType == 11)//bit
						value = kvp.Value.ToString().ToLower();
					else
						value = _BuildJson(kvp.Value);
					if (props.Length > 0)
						content.Append("\"" + props[0][Alias.Module.Object.Property.Name] + "\":" + value);
					else
						content.Append("\"" + kvp.Key + "\":" + value);
				}
				content.Append("}");

				return content.ToString();
			}
			else if (result is Dictionary<string, string>)
			{
				StringBuilder content = new StringBuilder();

				content.Append("{");
				foreach (KeyValuePair<string, string> kvp in (result as Dictionary<string, string>))
				{
					if (content.Length > 1)
						content.Append(",");

					int oledbType = Convert.ToInt32(System.Data.OleDb.OleDbType.VarWChar);
					DataRow[] props = _properties.Select(Alias.Module.Object.Property.UniqueInnerID + "='" + kvp.Key + "'");
					if (props.Length > 0)
						oledbType = Convert.ToInt32(props[0][Alias.Module.Object.Property.OleDbType]);
					string value = "";
					if ((oledbType == 3 || oledbType == 6 || oledbType == 131) && kvp.Value.ToString() != "")//int, moeny, decimal
						value = kvp.Value.ToString();
					else if (oledbType == 11)//bit
						value = kvp.Value.ToString().ToLower();
					else
						value = _BuildJson(kvp.Value);
					if (props.Length > 0)
						content.Append("\"" + props[0][Alias.Module.Object.Property.Name] + "\":" + value);
					else
						content.Append("\"" + kvp.Key + "\":" + value);
				}
				content.Append("}");

				return content.ToString();
			}
			else if (result is DataTable)
			{
				StringBuilder content = new StringBuilder();

				content.Append("[");
				DataTable table = (DataTable)result;
				foreach (DataRow row in table.Rows)
				{
					if (content.Length > 1)
						content.Append(",");

					content.Append("{");
					for (int i = 0; i < table.Columns.Count; i++)
					{
						if (i > 0)
							content.Append(",");

                        string name = null;
                        string value = null;
                        DataRow[] props = _properties.Select(Alias.Module.Object.Property.UniqueInnerID + "='" + table.Columns[i].ColumnName + "'");
                        if (props.Length == 0)
                        {
                            name = table.Columns[i].ColumnName;

                            Type type = table.Columns[i].DataType;
                            if (type == typeof(int) || type == typeof(Decimal))
                                value = row[table.Columns[i].ColumnName].ToString();
                            else if (type == typeof(bool))
                                value = row[table.Columns[i].ColumnName].ToString().ToLower();
                        }
                        else
                        {
                            name = props[0][Alias.Module.Object.Property.Name].ToString();

                            int oledbType = Convert.ToInt32(props[0][Alias.Module.Object.Property.OleDbType]);
						    if ((oledbType == 3 || oledbType == 6 || oledbType == 131) && row[table.Columns[i].ColumnName].ToString() != "")//int, moeny, decimal
							    value = row[table.Columns[i].ColumnName].ToString();
						    else if (oledbType == 11)//bit
							    value = row[table.Columns[i].ColumnName].ToString().ToLower();
                        }
                        if (value == null)
                            value = _BuildJson(row[table.Columns[i].ColumnName]);
                        content.Append("\"" + name + "\":" + value);
					}
					content.Append("}");
				}
				content.Append("]");

				return content.ToString();
			}
			else if (result is List<Dictionary<string, object>>)
			{
				StringBuilder content = new StringBuilder();

				content.Append("[");
				List<Dictionary<string, object>> list = (List<Dictionary<string, object>>)result;
				foreach (Dictionary<string, object> row in list)
				{
					if (content.Length > 1)
						content.Append(",");

					content.Append(_BuildJson(row));
				}
				content.Append("]");

				return content.ToString();
			}
			else if (result is object[])
			{
				StringBuilder content = new StringBuilder();

				content.Append("[");
				foreach (object item in result as object[])
				{
					if (content.Length > 1)
						content.Append(",");

					content.Append(_BuildJson(item));
				}
				content.Append("]");

				return content.ToString();
			}
			else if (result is int || result is long || result is float || result is decimal || result is double)
			{
				return result.ToString();
			}
			else if (result is bool)
			{
				return ((bool)result) ? "true" : "false";
			}
            else if (result is DateTime)
            {
                return "\"" + ((DateTime)result).ToString("yyyy-MM-dd HH:mm:ss") + "\"";
            }
			else
			{
				return "\"" + result.ToString().Replace("\0", "").Replace("\\", "\\\\").Replace("\"", "\\\"").Replace("\r", "\\r").Replace("\n", "\\n").Replace("\t", "\\t") + "\"";
			}
		}

		//作用：方便于写入日志
		public void _Log(String title, String message, String lang)
		{
			using (ILog bll = (ILog)CreateObject("Log", 0, lang, false))
			{
				Dictionary<string, object> item = new Dictionary<string, object>();
				if (String.IsNullOrEmpty(title))
					item[Alias.Log.URL] = HttpContext.Current.Request.Url;
				else
					item[Alias.Log.URL] = title + "：" + HttpContext.Current.Request.Url;
				item[Alias.Log.Message] = message;
				bll.Add(item);
			}
		}

		#region 智能更新
		private void _SmpartUpdate(Dictionary<string, object> update)
		{
			if (Convert.ToBoolean(_site[SystemModule.Alias.InstanceConfig.DisableSmartUpdate]))
				return;

			string root = MapPath(GetCachePath() + "html");
			if (!Directory.Exists(root))
				return;

			//OleDbConnection connection = null;
			//OleDbCommand command = null;

			#region 功能栏目
			DataRow[] channels = Channels.Select(Alias.Template.IsAdmin + "=false AND " + Alias.Channel.TemplatePath + " IS NOT NULL AND " + Alias.Channel.Lang + "='" + update["Lang"] + "' AND (" + Alias.Instance.ID + " IS NULL OR " + Alias.Instance.InnerID + "=" + update["InstanceID"] + ")");
			foreach (DataRow channel in channels)
			{
				DataRow[] pages = Pages.Select(Alias.Template.Page.TemplatePath + "='" + channel[Alias.Channel.TemplatePath] + "' AND " + Alias.Template.Page.Object + "='" + update["Object"] + "'" + (Convert.IsDBNull(update["PageTemplate"]) ? "" : " AND " + Alias.Template.Page.PathInTemplate + "='" + update["PageTemplate"] + "'"));
				foreach (DataRow page in pages)
				{
					#region 页面的Mark属性指定了，并且对应的栏目实例不是本实例的话，停止处理当前页面
					if (!String.IsNullOrEmpty(Convert.ToString(page[Alias.Template.Page.Mark])))
					{
						DataRow markChannel = GetExtraInstance(channel, page[Alias.Template.Page.Mark].ToString());
						if (markChannel == null || !int.Equals(markChannel[Alias.Instance.InnerID], update["InstanceID"]))
							continue;
					}
					#endregion

					#region PathInChannel
					string pathInChannel = page[Alias.Template.Page.PathInChannel].ToString();
					if (pathInChannel == String.Empty)
					{
						pathInChannel = page[Alias.Template.Page.PathInTemplate].ToString().Replace(".dwt", "");
						if (pathInChannel.EndsWith(".e"))
							pathInChannel = pathInChannel.Remove(pathInChannel.Length - 2);
					}
					pathInChannel = pathInChannel.Replace("{ID}", update["PageID"].ToString());
					pathInChannel = pathInChannel.Replace("{Ext}", channel[Alias.Channel.Extension].ToString());
					#endregion

					#region 是否生成，如果不生成，则继续下一个
					string runAt = page[Alias.Template.Page.RunAt].ToString();
					bool generate = false;
					if (runAt == "Generate")
						generate = true;
					else if (runAt != "Execute" && !pathInChannel.EndsWith(".ascx"))
						generate = Convert.ToBoolean(channel[Alias.Channel.Generate]);
					if (!generate)
						continue;
					#endregion

					#region 搜索匹配的文件，并且修改创建时间，以使它超时

					StringBuilder pattern = new StringBuilder();

					string channelPath = channel[Alias.Channel.Path].ToString();
					int colon = channelPath.IndexOf('{');
					if (colon != -1)
					{
						int slash = channelPath.LastIndexOf('/', colon);
						pattern.Append(channelPath.Substring(slash));
						channelPath = channelPath.Substring(0, slash);
					}

					channelPath = root + channelPath.Replace('/', '\\');
					if (Directory.Exists(channelPath))
					{
						pattern.Append(Regex.Replace(pathInChannel.Replace(".", "\\."), @"\{\w+:(.*?)(?<!\\)\}", "$1").Replace(@"\}", "}"));
						pattern.Replace("{ID}", @"\d+");
						pattern.Replace("{Page}", @"(_\d+)?");
						pattern.Append(@"(\.gz)?");

						string fileName;
						int slash = pathInChannel.LastIndexOf('/');
						if (slash == -1)
							fileName = pathInChannel;
						else
							fileName = pathInChannel.Substring(slash + 1);
						fileName = Regex.Replace(fileName, @"\{.*?\}", "*");
						fileName = Regex.Replace(fileName, @"\*+", "*");
						string[] files = Directory.GetFiles(channelPath, fileName, SearchOption.AllDirectories);
						foreach (string file in files)
						{
							string path = file.Substring(channelPath.Length).Replace('\\', '/');
							if (Regex.IsMatch(path, pattern.ToString(), RegexOptions.IgnoreCase))
							{
								try
								{
									FileInfo fi = new FileInfo(file);
									fi.LastWriteTime = DateTime.Now.AddYears(-1);
								}
								catch
								{
								}
							}
						}
					}
					#endregion

					#region 阿里加速器
					/*注：不再考虑阿里加速器。彻底没有问题时，才移除下面注释的代码
					if (!String.IsNullOrEmpty(Site[Alias.InstanceConfig.AliCache]))
					{
						string url = Site[Alias.InstanceConfig.Domain];
						url = url.TrimEnd('/') + pathInChannel.Replace("{Page}", @"(_\d+)?");
						url = Regex.Replace(url, @"\{\w+:(.*?)(?<!\\)\}", "$1").Replace(@"\}", "}");

						bool isRegex = pathInChannel.IndexOf('{') != -1;

						if (connection == null)
							connection = new OleDbConnection();
						if (command == null)
							command = new OleDbCommand();

						connection.ConnectionString = Site[Alias.InstanceConfig.AliCache];
						connection.Open();
						command.Connection = connection;

						command.CommandText = "DELETE FROM [Update] WHERE [Url]='" + url + "'";
						command.ExecuteNonQuery();

						command.CommandText = "INSERT INTO [Update]([Url], [IsRegex]) VALUES(?, ?)";
						command.Parameters.Clear();
						command.Parameters.Add("@Url", OleDbType.VarWChar).Value = url;
						command.Parameters.Add("@IsRegex", OleDbType.Boolean).Value = isRegex;
						command.ExecuteNonQuery();

						connection.Close();
					}
					*/
					#endregion
				}
			}
			#endregion

			#region 非功能栏目
			channels = Channels.Select(Alias.Template.IsAdmin + "=false AND " + Alias.Channel.TemplatePath + " IS NOT NULL" + (String.IsNullOrEmpty((string)update["Lang"]) ? "" : " AND " + Alias.Channel.Lang + "='" + update["Lang"] + "'"));
			foreach (DataRow channel in channels)
			{
				DataRow[] pages = Pages.Select(Alias.Template.Page.TemplatePath + "='" + channel[Alias.Channel.TemplatePath] + "' AND " + Alias.Template.Page.Object + "=''");
				foreach (DataRow page in pages)
				{
					#region PathInChannel
					string pathInChannel = page[Alias.Template.Page.PathInChannel].ToString();
					if (pathInChannel == String.Empty)
					{
						pathInChannel = page[Alias.Template.Page.PathInTemplate].ToString().Replace(".dwt", "");
						if (pathInChannel.EndsWith(".e"))
							pathInChannel = pathInChannel.Remove(pathInChannel.Length - 2);
					}
					pathInChannel = pathInChannel.Replace("{ID}", update["PageID"].ToString());
					pathInChannel = pathInChannel.Replace("{Ext}", channel[Alias.Channel.Extension].ToString());
					#endregion

					#region 是否生成，如果不生成，则继续下一个
					string runAt = page[Alias.Template.Page.RunAt].ToString();
					bool generate = false;
					if (runAt == "Generate")
						generate = true;
					else if (runAt != "Execute" && !pathInChannel.EndsWith(".ascx"))
						generate = Convert.ToBoolean(channel[Alias.Channel.Generate]);
					if (!generate)
						continue;
					#endregion

					#region 搜索匹配的文件，并且修改创建时间，以使它超时

					StringBuilder pattern = new StringBuilder();

					string channelPath = channel[Alias.Channel.Path].ToString();
					int colon = channelPath.IndexOf('{');
					if (colon != -1)
					{
						int slash = channelPath.LastIndexOf('/', colon);
						pattern.Append(channelPath.Substring(slash));
						channelPath = channelPath.Substring(0, slash);
					}

					channelPath = root + channelPath.Replace('/', '\\');
					if (Directory.Exists(channelPath))
					{
						pattern.Append(Regex.Replace(pathInChannel.Replace(".", "\\."), @"\{\w+:(.*?)(?<!\\)\}", "$1").Replace(@"\}", "}"));
						pattern.Replace("{ID}", @"\d+");
						pattern.Replace("{Page}", @"(_\d+)?");
						pattern.Append(@"(\.gz)?");

						string fileName;
						int slash = pathInChannel.LastIndexOf('/');
						if (slash == -1)
							fileName = pathInChannel;
						else
							fileName = pathInChannel.Substring(slash + 1);
						fileName = Regex.Replace(fileName, @"\{.*?\}", "*");
						fileName = Regex.Replace(fileName, @"\*+", "*");
						string[] files = Directory.GetFiles(channelPath, fileName, SearchOption.AllDirectories);
						foreach (string file in files)
						{
							string path = file.Substring(channelPath.Length).Replace('\\', '/');
							if (Regex.IsMatch(path, pattern.ToString(), RegexOptions.IgnoreCase))
							{
								try
								{
									FileInfo fi = new FileInfo(file);
									fi.LastWriteTime = DateTime.Now.AddYears(-1);
								}
								catch
								{
								}
							}
						}
					}
					#endregion

					#region 阿里加速器
					/*注：不再考虑阿里加速器。彻底没有问题时，才移除下面注释的代码
					if (!String.IsNullOrEmpty(Site[Alias.InstanceConfig.AliCache]))
					{
						string url = Site[Alias.InstanceConfig.Domain];
						url = url.TrimEnd('/') + pathInChannel.Replace("{Page}", @"(_\d+)?");
						url = Regex.Replace(url, @"\{\w+:(.*?)(?<!\\)\}", "$1").Replace(@"\}", "}");

						bool isRegex = pathInChannel.IndexOf('{') != -1;

						if (connection == null)
							connection = new OleDbConnection();
						if (command == null)
							command = new OleDbCommand();

						connection.ConnectionString = Site[Alias.InstanceConfig.AliCache];
						connection.Open();
						command.Connection = connection;

						command.CommandText = "DELETE FROM [Update] WHERE [Url]='" + url + "'";
						command.ExecuteNonQuery();

						command.CommandText = "INSERT INTO [Update]([Url], [IsRegex]) VALUES(?, ?)";
						command.Parameters.Clear();
						command.Parameters.Add("@Url", OleDbType.VarWChar).Value = url;
						command.Parameters.Add("@IsRegex", OleDbType.Boolean).Value = isRegex;
						command.ExecuteNonQuery();

						connection.Close();
					}
					*/
					#endregion
				}
			}
			#endregion
		}
		private void _ClearAllCache()
        {
			string root = MapPath(GetCachePath() + "html");
			if (Directory.Exists(root))
			{
				string[] files = Directory.GetFiles(root, "*", SearchOption.AllDirectories);
				foreach (string file in files)
				{
					try
					{
						FileInfo fi = new FileInfo(file);
						fi.LastWriteTime = DateTime.Now.AddYears(-1);
					}
					catch
					{
					}
				}
			}

			#region 清空加速器的所有缓存
			/*注：不再考虑阿里加速器。彻底没有问题时，才移除下面注释的代码
			if (!String.IsNullOrEmpty(_site[Alias.InstanceConfig.AliCache]))
            {
                OleDbConnection connection = new OleDbConnection();
                OleDbCommand command = new OleDbCommand();

				connection.ConnectionString = _site[Alias.InstanceConfig.AliCache];
                connection.Open();
                command.Connection = connection;

                command.CommandText = "DELETE FROM [Update]";
                command.ExecuteNonQuery();

                command.CommandText = "INSERT INTO [Update]([Url]) VALUES('*')";
                command.ExecuteNonQuery();

                connection.Close();
            }
			*/
            #endregion
        }
		#endregion

		private string _Translate(string text, string from, string to)
        {
            if (from == "UYG" && to == "UKY")
                return TranslateFromUYGToUKY(text);
            else if (from == "CHS" && to == "CHT")
                return TranslateFromCHSToCHT(text);
            else if (from == "UYG" && to == "UYL")
                return TranslateFromUYGToUYL(text);
            else if (from == "UYG" && to == "UYC")
                return TranslateFromUYGToUYC(text);
            else if (from == "KGA" && to == "KGC")
                return TranslateFromKGAToKGC(text);
            else
                return text;
        }
        private string TranslateFromUYGToUKY(string text)
        {
            StringBuilder result = new StringBuilder(text.Length);
            foreach (char c in text)
            {
                switch (c)
                {
                    case '\u06BE'://a
                        result.Append('h');
                        break;
                    case '\u0628'://b
                        result.Append('b');
                        break;
                    case '\u063A'://c
                        result.Append("gh");
                        break;
                    case '\u062F'://d
                        result.Append('d');
                        break;
                    case '\u0698'://D
                        result.Append('z');
                        break;
                    case '\u06D0'://e
                        result.Append('e');
                        break;
                    case '\u0627'://f
                        result.Append('a');
                        break;
                    case '\u0641'://F
                        result.Append('f');
                        break;
                    case '\u06D5'://g
                        result.Append('a');
                        break;
                    case '\u06AF'://G
                        result.Append('g');
                        break;
                    case '\u0649'://h
                        result.Append('i');
                        break;
                    case '\u062E'://H
                        result.Append('h');
                        break;
                    case '\u06AD'://i
                        result.Append("ng");
                        break;
                    case '\u0642'://j
                        result.Append("k");
                        break;
                    case '\u062C'://J
                        result.Append('j');
                        break;
                    case '\u0643'://k
                        result.Append('k');
                        break;
                    case '\u06C6'://K
                        result.Append('o');
                        break;
                    case '\u0644'://l
                        result.Append('l');
                        break;
                    case '\u0645'://m
                        result.Append('m');
                        break;
                    case '\u0646'://n
                        result.Append('n');
                        break;
                    case '\u0648'://o
                        result.Append('o');
                        break;
                    case '\u067E'://p
                        result.Append('p');
                        break;
                    case '\u0686'://q
                        result.Append('q');
                        break;
                    case '\u0631'://r
                        result.Append('r');
                        break;
                    case '\u0633'://s
                        result.Append('s');
                        break;
                    case '\u062A'://t
                        result.Append('t');
                        break;
                    case '\u06C7'://u
                        result.Append('u');
                        break;
                    case '\u06C8'://v
                        result.Append('u');
                        break;
                    case '\u06CB'://w
                        result.Append('w');
                        break;
                    case '\u0634'://x
                        result.Append('x');
                        break;
                    case '\u064A'://y
                        result.Append('y');
                        break;
                    case '\u0632'://z
                        result.Append('z');
                        break;
                    case '\u060C'://,
                        result.Append(',');
                        break;
                    case '\u061F'://?
                        result.Append('?');
                        break;
                    case '\u061B'://;
                        result.Append(';');
                        break;
                    default:
                        if (c < 0x0600 || c > 0x06FF)
                            result.Append(c);
                        break;
                }
            }

            return result.ToString();
        }
        private string TranslateFromCHSToCHT(string text)
        {
            StringBuilder result = new StringBuilder(text.Length);
            foreach (char c in text)
            {
                if (c < '\u4E00' || c > '\u9FA5')
                {
                    result.Append(c);
                    continue;
                }

                int index = simpleChinese.IndexOf(c);
                if (index == -1)
                    result.Append(c);
                else
                    result.Append(traditionalChinese[index]);
            }

            return result.ToString();
        }
        private string TranslateFromUYGToUYL(string text)
        {
            StringBuilder result = new StringBuilder(text.Length);
            for (int i = 0; i < text.Length; i++)
            {
                char c = text[i];
                if (c < '\u0600' || c > '\u06FF')
                {
                    result.Append(c);
                    continue;
                }

                switch (c)
                {
                    case 'ئ':
                        if (i > 0)
                        {
					        char prev = text[i - 1];
					        if ((prev >= '\u0600' && prev <= '\u06FF') && (prev != '،' && prev != '؟' && prev != '؛'))
						        result.Append('\'');
                        }
                        break;
			        case 'ا':
				        result.Append('a');
				        break;
			        case 'ە':
				        result.Append('e');
				        break;
			        case 'ب':
				        result.Append('b');
				        break;
			        case 'پ':
				        result.Append('p');
				        break;
			        case 'ت':
				        result.Append('t');
				        break;
			        case 'ج':
				        result.Append('j');
				        break;
			        case 'چ':
				        result.Append("ch");
				        break;
			        case 'خ':
				        result.Append('x');
				        break;
			        case 'د':
				        result.Append('d');
				        break;
			        case 'ر':
				        result.Append('r');
				        break;
			        case 'ز':
				        result.Append('z');
				        break;
			        case 'ژ':
				        result.Append('z');
				        result.Append('h');
				        break;
			        case 'ش':
				        result.Append('s');
				        result.Append('h');
				        break;
			        case 'س':
				        if ((i + 1) < text.Length && text[i + 1] == 'ھ')
				        {
					        result.Append("s'h");
					        i++;
				        }
				        else
				        {
					        result.Append('s');
				        }
				        break;
			        case 'ف':
				        result.Append('f');
				        break;
			        case 'ق':
				        result.Append('q');
				        break;
			        case 'ك':
				        result.Append('k');
				        break;
			        case 'ن':
				        if ((i + 1) < text.Length && text[i + 1] == 'غ')
				        {
					        result.Append("n'gh");
					        i++;
				        }
				        else if ((i + 1) < text.Length && text[i + 1] == 'گ')
				        {
					        result.Append("n'g");
					        i++;
				        }
				        else
				        {
					        result.Append('n');
				        }
				        break;
			        case 'غ':
				        result.Append("gh");
				        break;
			        case 'ڭ':
				        if ((i + 1) < text.Length && text[i + 1] == 'ھ')
				        {
                            result.Append("ng'h");
					        i++;
				        }
				        else
				        {
                            result.Append("ng");
				        }
				        break;
			        case 'گ':
				        result.Append('g');
				        break;
			        case 'ل':
				        result.Append('l');
				        break;
			        case 'م':
				        result.Append('m');
				        break;
			        case 'ھ':
				        result.Append('h');
				        break;
			        case 'و':
				        result.Append('o');
				        break;
			        case 'ۇ':
				        result.Append('u');
				        break;
			        case 'ۆ':
				        result.Append('ö');
				        break;
			        case 'ۈ':
				        result.Append('ü');
				        break;
			        case 'ۋ':
				        result.Append('w');
				        break;
			        case 'ې':
                        result.Append('ë');
				        break;
			        case 'ى':
				        result.Append('i');
				        break;
			        case 'ي':
				        result.Append('y');
				        break;
			        case '،':
				        result.Append(',');
				        break;
			        case '؟':
				        result.Append('?');
				        break;
			        case '؛':
				        result.Append(';');
				        break;
                    default:
                        result.Append(c);
                        break;
                }
            }

            return result.ToString();
        }
        private string TranslateFromUYGToUYC(string text)
        {
            StringBuilder result = new StringBuilder(text.Length);
            for (int i = 0; i < text.Length; i++)
            {
                char c = text[i];
                if (c < '\u0600' || c > '\u06FF')
                {
                    result.Append(c);
                    continue;
                }

                switch (c)
                {
			        case 'ئ':
				        if (i > 0)
				        {
					        char prev = text[i - 1];
					        if ((prev >= '\u0600' && prev <= '\u06FF') && (prev != '،' && prev != '؟' && prev != '؛'))
						        result.Append('ъ');
				        }
				        break;
			        case 'ا':
				        result.Append('а');
				        break;
			        case 'ە':
				        result.Append('ә');
				        break;
			        case 'ب':
				        result.Append('б');
				        break;
			        case 'پ':
				        result.Append('п');
				        break;
			        case 'ت':
				        result.Append('т');
				        break;
			        case 'ج':
				        result.Append('җ');
				        break;
			        case 'چ':
				        result.Append('ч');
				        break;
			        case 'خ':
				        result.Append('х');
				        break;
			        case 'د':
				        result.Append('д');
				        break;
			        case 'ر':
				        result.Append('р');
				        break;
			        case 'ز':
				        result.Append('з');
				        break;
			        case 'ژ':
				        result.Append('ж');
				        break;
			        case 'ش':
				        result.Append('ш');
				        break;
			        case 'س':
				        result.Append('с');
				        break;
			        case 'ف':
				        result.Append('ф');
				        break;
			        case 'ق':
				        result.Append('қ');
				        break;
			        case 'ك':
				        result.Append('к');
				        break;
			        case 'ن':
				        result.Append('н');
				        break;
			        case 'غ':
				        result.Append('ғ');
				        break;
			        case 'ڭ':
				        result.Append('ң');
				        break;
			        case 'گ':
				        result.Append('г');
				        break;
			        case 'ل':
				        result.Append('л');
				        break;
			        case 'م':
				        result.Append('м');
				        break;
			        case 'ھ':
				        result.Append('һ');
				        break;
			        case 'و':
				        result.Append('о');
				        break;
			        case 'ۇ':
				        result.Append('у');
				        break;
			        case 'ۆ':
				        result.Append('ө');
				        break;
			        case 'ۈ':
				        result.Append('ү');
				        break;
			        case 'ۋ':
				        result.Append('в');
				        break;
			        case 'ې':
				        result.Append('е');
				        break;
			        case 'ى':
				        result.Append('и');
				        break;
			        case 'ي':
				        if ((i + 1) < text.Length && text[i + 1] == 'ا')
				        {
					        result.Append('я');
					        i++;
				        }
				        else if ((i + 1) < text.Length && text[i + 1] == 'ۇ')
				        {
					        result.Append('ю');
					        i++;
				        }
				        else if ((i + 1) < text.Length && text[i + 1] == 'و')
				        {
					        result.Append('ё');
					        i++;
				        }
				        else
				        {
					        result.Append('й');
				        }
				        break;
			        case '،':
				        result.Append(',');
				        break;
			        case '؟':
				        result.Append('?');
				        break;
			        case '؛':
				        result.Append(';');
				        break;
			        default:
				        result.Append(c);
                        break;
                }
            }

            return result.ToString();
        }
        private string TranslateFromKGAToKGC(string text)
        {
            StringBuilder result = new StringBuilder(text.Length);
            for (int i = 0; i < text.Length; i++)
            {
                char c = text[i];
                if (c < '\u0600' || c > '\u06FF')
                {
                    result.Append(c);
                    continue;
                }

                switch (c)
                {
                    case 'ئ':
                        result.Append('и');
                        break;
                    case 'ا':
                        result.Append('а');
                        break;
                    case 'ب':
                        result.Append('б');
                        break;
                    case 'ۋ':
                        result.Append('в');
                        break;
                    case 'گ':
                    case 'ع':
                        result.Append('г');
                        break;
                    case 'د':
                        result.Append('д');
                        break;
                    case 'ە':
                        if (i > 0)
                        {
                            char prev = text[i - 1];
                            if ((prev >= '\u0600' && prev <= '\u06FF') && (prev != '،' && prev != '؟' && prev != '؛'))
                                result.Append('е');
                            else
                                result.Append('э');
                        }
                        else
                        {
                            result.Append('э');
                        }
                        break;
                    case 'ي':
                        if ((i + 1) < text.Length && text[i + 1] == 'ا')
                        {
                            result.Append('я');
                            i++;
                        }
                        else if ((i + 1) < text.Length && text[i + 1] == 'ۇ')
                        {
                            result.Append('ю');
                            i++;
                        }
                        else if ((i + 1) < text.Length && text[i + 1] == 'و')
                        {
                            result.Append('ё');
                            i++;
                        }
                        else
                        {
                            result.Append('й');
                        }
                        break;
                    case 'ج':
                        result.Append('ж');
                        break;
                    case 'ز':
                        result.Append('з');
                        break;
                    case 'ك':
                    case 'ق':
                        result.Append('к');
                        break;
                    case 'ل':
                        result.Append('л');
                        break;
                    case 'م':
                        result.Append('м');
                        break;
                    case 'ن':
                        result.Append('н');
                        break;
                    case 'ڭ':
                        result.Append('ң');
                        break;
                    case 'و':
                        result.Append('о');
                        break;
                    case 'ۅ':
                        result.Append('ө');
                        break;
                    case 'پ':
                        result.Append('п');
                        break;
                    case 'ر':
                        result.Append('р');
                        break;
                    case 'س':
                        result.Append('с');
                        break;
                    case 'ت':
                        if ((i + 1) < text.Length && text[i + 1] == 'س')
                        {
                            result.Append('ц');
                            i++;
                        }
                        else
                        {
                            result.Append('т');
                        }
                        break;
                    case 'ۇ':
                        result.Append('у');
                        break;
                    case 'ۉ':
                        result.Append('ү');
                        break;
                    case 'ف':
                        result.Append('ф');
                        break;
                    case 'ح':
                        result.Append('х');
                        break;
                    case 'چ':
                        result.Append('ч');
                        break;
                    case 'ش':
                        if ((i + 2) < text.Length && text[i + 1] == 'ي' && text[i + 2] == 'ا')
                        {
                            result.Append('щ');
                            i++;
                        }
                        else
                        {
                            result.Append('ш');
                        }
                        break;
                    case 'ى':
                        result.Append('ы');
                        break;
                    case '،':
                        result.Append(',');
                        break;
                    case '؟':
                        result.Append('?');
                        break;
                    case '؛':
                        result.Append(';');
                        break;
                    default:
                        result.Append(c);
                        break;
                }
            }

            return result.ToString();
        }

        private void _DeleteFolder(string path, bool recursive)
        {
            string[] files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
            foreach (string file in files)
            {
                if ((File.GetAttributes(file) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                    File.SetAttributes(file, FileAttributes.Normal);
                File.Delete(file);
            }

            //string[] directories = Directory.GetDirectories(path, "*", SearchOption.AllDirectories);
            //foreach (string directory in directories)
            //{
            //    Directory.Delete(directory, true);
            //}

            Directory.Delete(path, true);
        }
        private string _Escape(object text)
        {
            StringBuilder result = new StringBuilder(HttpUtility.UrlEncodeUnicode(text.ToString()));
            result.Replace("+", "%20");
            return result.ToString();
        }
        private bool _HasUyghurText(object text)
        {
            foreach (char c in text.ToString())
            {
                if (c >= 0x0600 && c <= 0x06FF)
                    return true;
            }

            return false;
        }
        private string _HtmlToText(object html)
        {
            return Regex.Replace(html.ToString(), @"(<style(.|\s)*?</style>)|(<script(.|\s)*?</script>)|(<!--(.|\s)?-->)|(<[^>]*>)", "", RegexOptions.IgnoreCase);
        }

        private string _EncodeLogin(Dictionary<string, string> profile)
        {
            #region key
			string key = profile["ID"].Substring(0, 8);
			/*
            string key = String.Empty;
			string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
			// If there is no proxy, get the standard remote address
			if ((ip == null) || (ip == "") || (ip.ToLower() == "unknown"))
				ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
			ip = ip.Replace(".", "");
            foreach (char c in ip)
            {
                key += Convert.ToString((int)c);

                if (key.Length >= 8)
                {
                    key = key.Substring(0, 8);
                    break;
                }
            }
			*/
			#endregion

			#region iv
			string iv = String.Empty;
            string domain = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
			Match match = Regex.Match(domain, @"(^|\.)(?<v>\w+\.(com\.cn|net\.cn|org\.cn|gov\.cn|[a-z]+))$", RegexOptions.IgnoreCase);
			if (match.Success)
				domain = match.Groups["v"].Value;
			foreach (char c in domain)
            {
                iv += Convert.ToString((int)c);

                if (iv.Length >= 8)
                {
                    iv = iv.Substring(0, 8);
                    break;
                }
            }
            #endregion

            #region 数据
            string data = null;
            foreach (KeyValuePair<string, string> kvp in profile)
            {
                if (data != null)
                    data += "&";

                data += kvp.Key + "=" + _Escape(kvp.Value);
            }
            #endregion

            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            ICryptoTransform transform = des.CreateEncryptor(Encoding.UTF8.GetBytes(key), Encoding.UTF8.GetBytes(iv));
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write);

            StreamWriter sw = new StreamWriter(cs, Encoding.UTF8);
            sw.Write(data);
            sw.Close();

            string result = Convert.ToBase64String(ms.ToArray());

            cs.Close();
            ms.Close();
            des.Clear();

            return result;
        }
        private Dictionary<string, string> _DecodeLogin(string id, string text)
        {
            Dictionary<string, string> result = null;

            try
            {
                #region key
				string key = id.Substring(0, 8);
				/*
                string key = String.Empty;
				string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
				// If there is no proxy, get the standard remote address
				if ((ip == null) || (ip == "") || (ip.ToLower() == "unknown"))
					ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
				ip = ip.Replace(".", "");
				foreach (char c in ip)
                {
                    key += Convert.ToString((int)c);

                    if (key.Length >= 8)
                    {
                        key = key.Substring(0, 8);
                        break;
                    }
                }
				*/
                #endregion

                #region iv
                string iv = String.Empty;
                string domain = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
				Match match = Regex.Match(domain, @"(^|\.)(?<v>\w+\.(com\.cn|net\.cn|org\.cn|gov\.cn|[a-z]+))$", RegexOptions.IgnoreCase);
				if (match.Success)
					domain = match.Groups["v"].Value;
				foreach (char c in domain)
                {
                    iv += Convert.ToString((int)c);

                    if (iv.Length >= 8)
                    {
                        iv = iv.Substring(0, 8);
                        break;
                    }
                }
                #endregion

                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                ICryptoTransform transform = des.CreateDecryptor(Encoding.UTF8.GetBytes(key), Encoding.UTF8.GetBytes(iv));
                MemoryStream ms = new MemoryStream(Convert.FromBase64String(text));
                CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Read);
                StreamReader sr = new StreamReader(cs, Encoding.UTF8);

                string value = sr.ReadToEnd();
                string[] pairs = value.Split('&');
                foreach (string pair in pairs)
                {
                    if (result == null)
                        result = new Dictionary<string, string>();

                    string[] nameValue = pair.Split('=');
                    result[nameValue[0]] = HttpUtility.UrlDecode(nameValue[1]);
                }
                sr.Close();
                cs.Close();
                ms.Close();
                des.Clear();
            }
            catch
            {
                result = null;
            }

            return result;
        }
        private void _Login(Dictionary<string, string> profile, int minutes, bool isRootDomain)
        {
            HttpCookie cookie = null;
            string path = HttpContext.Current.Request.ApplicationPath;
            DateTime expires = DateTime.Now.AddMinutes(minutes);
            string domain = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
            if (isRootDomain)
            {
                Match match = Regex.Match(domain, @"(^|\.)(?<v>\w+\.(com\.cn|net\.cn|org\.cn|gov\.cn|[a-z]+))$", RegexOptions.IgnoreCase);
                if (match.Success)
                    domain = match.Groups["v"].Value;
            }

            cookie = new HttpCookie("UserName", HttpUtility.UrlEncodeUnicode((profile["Name"])));
            cookie.Path = path;
            cookie.Domain = domain;
            if (minutes > 0) cookie.Expires = expires;
            HttpContext.Current.Response.SetCookie(cookie);

            cookie = new HttpCookie("UserID", HttpUtility.UrlEncodeUnicode(profile["ID"]));
            cookie.Path = path;
            cookie.Domain = domain;
            if (minutes > 0) cookie.Expires = expires;
            HttpContext.Current.Response.SetCookie(cookie);

            cookie = new HttpCookie("Namespace", HttpUtility.UrlEncodeUnicode(profile["Namespace"]));
            cookie.Path = path;
            cookie.Domain = domain;
            if (minutes > 0) cookie.Expires = expires;
            HttpContext.Current.Response.SetCookie(cookie);

            cookie = new HttpCookie(profile["ID"], HttpUtility.UrlEncodeUnicode(_EncodeLogin(profile)));
            cookie.Path = path;
            cookie.Domain = domain;
            if (minutes > 0) cookie.Expires = expires;
            HttpContext.Current.Response.SetCookie(cookie);
        }
        private void _Logout()
        {
            string domain = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
			Match match = Regex.Match(domain, @"(^|\.)(?<v>\w+\.(com\.cn|net\.cn|org\.cn|gov\.cn|[a-z]+))$", RegexOptions.IgnoreCase);
			if (match.Success)
				domain = match.Groups["v"].Value;

            HttpCookie cookie = HttpContext.Current.Request.Cookies["UserID"];
            if (cookie != null)
            {
                cookie = new HttpCookie(cookie.Value);
                cookie.Domain = domain;
                cookie.Expires = DateTime.Now.AddYears(-1);
                HttpContext.Current.Response.Cookies.Add(cookie);

            }

            cookie = new HttpCookie("UserID");
            cookie.Domain = domain;
            cookie.Expires = DateTime.Now.AddYears(-1);
            HttpContext.Current.Response.Cookies.Add(cookie);

            cookie = new HttpCookie("UserName");
            cookie.Domain = domain;
            cookie.Expires = DateTime.Now.AddYears(-1);
            HttpContext.Current.Response.Cookies.Add(cookie);

			cookie = new HttpCookie("Namespace");
            cookie.Domain = domain;
            cookie.Expires = DateTime.Now.AddYears(-1);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        private void _AddToUser(Dictionary<string, string> kvs)
        {
            Dictionary<string, string> user = _User;
            if (user == null)
                return;

            HttpCookie cookie = HttpContext.Current.Request.Cookies["UserID"];
            foreach (KeyValuePair<string, string> kv in kvs)
                user[kv.Key] = kv.Value;
            Login(user, cookie.Expires == null ? 0 : (int)cookie.Expires.Subtract(DateTime.Now).TotalMinutes);
        }

        internal bool IsInitialized()
        {
            return WaitHandle.WaitAll(_initializedEvents, 60000, false);
        }

        #region Properties
        private Dictionary<string, string> _User
        {
            get
            {
                if (HttpContext.Current == null)
                    return null;

				Dictionary<string, string> profile = null;
                HttpRequest request = HttpContext.Current.Request;
                HttpCookie cookie = request.Cookies["UserID"];
				if (cookie == null)
				{
					String userId = request.Headers["UserID"];
					String userSign = request.Headers["UserSign"];
					if (String.IsNullOrEmpty(userId) || String.IsNullOrEmpty(userSign))
						return null;

					profile = _DecodeLogin(userId, userSign);
				}
				else
				{
					String id = HttpUtility.UrlDecode(cookie.Value);
					cookie = request.Cookies[id];
					if (cookie == null)
						return null;

					profile = _DecodeLogin(id, HttpUtility.UrlDecode(cookie.Value));
				}

                if (profile == null)
                    return null;

                #region 区分会员和管理员（暂时去掉，因为前台用户严重依赖栏目所属模块）
                //string path = String.Empty;
                //if (request.Path.Equals(_GetWebPath() + "alicms.aspx", StringComparison.InvariantCultureIgnoreCase))
                //{
                //    path = request.UrlReferrer.LocalPath;
                //    if (request.UrlReferrer.Query.StartsWith("?/"))//fake404
                //        path = request.UrlReferrer.Query.Substring(1);
                //}
                //else
                //{
                //    path = request.Path;
                //}

                //DataRow channel = GetChannel(path);
                //if (channel == null)
                //    return profile;

                //if (!profile["Namespace"].StartsWith(channel[Alias.Module.Namespace].ToString()))
                //    return null;
                #endregion

                return profile;
            }
        }
        internal DataTable RewriteRules
        {
            get { return _rewriteRules; }
        }
		internal AutoResetEvent GenerateEvent
		{
			get
			{
				return _generateEvent;
			}
		}
		internal bool IsForbidden
		{
			get
			{
				return _isForbidden;
			}
		}
		internal string ForbiddenRedirect
		{
			get
			{
				return _forbiddenRedirect;
			}
		}
		internal DataTable Clients
		{
			get
			{
				return _clients;
			}
		}
		private bool _IsClient
		{
			get
			{
				HttpRequest request = HttpContext.Current.Request;
				return (request.QueryString["Action"] == "Upload" || request.QueryString["Action"] == "Client") && !String.IsNullOrEmpty(request.QueryString["CID"]) && !String.IsNullOrEmpty(request.QueryString["Sign"]);
			}
		}
        internal DataTable WhiteList
        {
            get { return _whiteList; }
        }
        internal DataTable BlackList
        {
			get { return _blackList; }
        }
		internal DataTable KeywordFilter
        {
			get { return _keywordFilter; }
        }
        #endregion

        #region Fields
        private DataTable _languages;
        private DataTable _channels;
        private DataTable _instances;
        private DataTable _extraInstances;
        private DataTable _pages;
        private DataTable _objects;
        private DataTable _properties;
        private DataTable _roles;
        private DataTable _privileges;
        private DataTable _permissions;
        private DataTable _rewriteRules;
        private DataTable _tasks;
		private DataTable _clients;
		private DataTable _whiteList;
		private DataTable _blackList;
		private DataTable _keywordFilter;
        private Dictionary<string, string> _site;
        private EventWaitHandle[] _initializedEvents;
		private AutoResetEvent _generateEvent;
        private Thread _taskThread;
        private string _webPath;
        private string _webAbsolutePath;
		private bool _isForbidden = false;
		private string _forbiddenRedirect;
        private const string simpleChinese = "啊阿埃挨哎唉哀皑癌蔼矮艾碍爱隘鞍氨安俺按暗岸胺案肮昂盎凹敖熬翱袄傲奥懊澳芭捌扒叭吧笆八疤巴拔跋靶把耙坝霸罢爸白柏百摆佰败拜稗斑班搬扳般颁板版扮拌伴瓣半办绊邦帮梆榜膀绑棒磅蚌镑傍谤苞胞包褒剥薄雹保堡饱宝抱报暴豹鲍爆杯碑悲卑北辈背贝钡倍狈备惫焙被奔苯本笨崩绷甭泵蹦迸逼鼻比鄙笔彼碧蓖蔽毕毙毖币庇痹闭敝弊必辟壁臂避陛鞭边编贬扁便变卞辨辩辫遍标彪膘表鳖憋别瘪彬斌濒滨宾摈兵冰柄丙秉饼炳病并玻菠播拨钵波博勃搏铂箔伯帛舶脖膊渤泊驳捕卜哺补埠不布步簿部怖擦猜裁材才财睬踩采彩菜蔡餐参蚕残惭惨灿苍舱仓沧藏操糙槽曹草厕策侧册测层蹭插叉茬茶查碴搽察岔差诧拆柴豺搀掺蝉馋谗缠铲产阐颤昌猖场尝常长偿肠厂敞畅唱倡超抄钞朝嘲潮巢吵炒车扯撤掣彻澈郴臣辰尘晨忱沉陈趁衬撑称城橙成呈乘程惩澄诚承逞骋秤吃痴持匙池迟弛驰耻齿侈尺赤翅斥炽充冲虫崇宠抽酬畴踌稠愁筹仇绸瞅丑臭初出橱厨躇锄雏滁除楚础储矗搐触处揣川穿椽传船喘串疮窗幢床闯创吹炊捶锤垂春椿醇唇淳纯蠢戳绰疵茨磁雌辞慈瓷词此刺赐次聪葱囱匆从丛凑粗醋簇促蹿篡窜摧崔催脆瘁粹淬翠村存寸磋撮搓措挫错搭达答瘩打大呆歹傣戴带殆代贷袋待逮怠耽担丹单郸掸胆旦氮但惮淡诞弹蛋当挡党荡档刀捣蹈倒岛祷导到稻悼道盗德得的蹬灯登等瞪凳邓堤低滴迪敌笛狄涤翟嫡抵底地蒂第帝弟递缔颠掂滇碘点典靛垫电佃甸店惦奠淀殿碉叼雕凋刁掉吊钓调跌爹碟蝶迭谍叠丁盯叮钉顶鼎锭定订丢东冬董懂动栋侗恫冻洞兜抖斗陡豆逗痘都督毒犊独读堵睹赌杜镀肚度渡妒端短锻段断缎堆兑队对墩吨蹲敦顿囤钝盾遁掇哆多夺垛躲朵跺舵剁惰堕蛾峨鹅俄额讹娥恶厄扼遏鄂饿恩而儿耳尔饵洱二贰发罚筏伐乏阀法珐藩帆番翻樊矾钒繁凡烦反返范贩犯饭泛坊芳方肪房防妨仿访纺放菲非啡飞肥匪诽吠肺废沸费芬酚吩氛分纷坟焚汾粉奋份忿愤粪丰封枫蜂峰锋风疯烽逢冯缝讽奉凤佛否夫敷肤孵扶拂辐幅氟符伏俘服浮涪福袱弗甫抚辅俯釜斧脯腑府腐赴副覆赋复傅付阜父腹负富讣附妇缚咐噶嘎该改概钙盖溉干甘杆柑竿肝赶感秆敢赣冈刚钢缸肛纲岗港杠篙皋高膏羔糕搞镐稿告哥歌搁戈鸽胳疙割革葛格蛤阁隔铬个各给根跟耕更庚羹埂耿梗工攻功恭龚供躬公宫弓巩汞拱贡共钩勾沟苟狗垢构购够辜菇咕箍估沽孤姑鼓古蛊骨谷股故顾固雇刮瓜剐寡挂褂乖拐怪棺关官冠观管馆罐惯灌贯光广逛瑰规圭硅归龟闺轨鬼诡癸桂柜跪贵刽辊滚棍锅郭国果裹过哈骸孩海氦亥害骇酣憨邯韩含涵寒函喊罕翰撼捍旱憾悍焊汗汉夯杭航壕嚎豪毫郝好耗号浩呵喝荷菏核禾和何合盒貉阂河涸赫褐鹤贺嘿黑痕很狠恨哼亨横衡恒轰哄烘虹鸿洪宏弘红喉侯猴吼厚候后呼乎忽瑚壶葫胡蝴狐糊湖弧虎唬护互沪户花哗华猾滑画划化话槐徊怀淮坏欢环桓还缓换患唤痪豢焕涣宦幻荒慌黄磺蝗簧皇凰惶煌晃幌恍谎灰挥辉徽恢蛔回毁悔慧卉惠晦贿秽会烩汇讳诲绘荤昏婚魂浑混豁活伙火获或惑霍货祸击圾基机畸稽积箕肌饥迹激讥鸡姬绩缉吉极棘辑籍集及急疾汲即嫉级挤几脊己蓟技冀季伎祭剂悸济寄寂计记既忌际妓继纪嘉枷夹佳家加荚颊贾甲钾假稼价架驾嫁歼监坚尖笺间煎兼肩艰奸缄茧检柬碱硷拣捡简俭剪减荐槛鉴践贱见键箭件健舰剑饯渐溅涧建僵姜将浆江疆蒋桨奖讲匠酱降蕉椒礁焦胶交郊浇骄娇嚼搅铰矫侥脚狡角饺缴绞剿教酵轿较叫窖揭接皆秸街阶截劫节茎睛晶鲸京惊精粳经井警景颈静境敬镜径痉靖竟竞净炯窘揪究纠玖韭久灸九酒厩救旧臼舅咎就疚鞠拘狙疽居驹菊局咀矩举沮聚拒据巨具距踞锯俱句惧炬剧捐鹃娟倦眷卷绢撅攫抉掘倔爵桔杰捷睫竭洁结解姐戒藉芥界借介疥诫届巾筋斤金今津襟紧锦仅谨进靳晋禁近烬浸尽劲荆兢觉决诀绝均菌钧军君峻俊竣浚郡骏喀咖卡咯开揩楷凯慨刊堪勘坎砍看康慷糠扛抗亢炕考拷烤靠坷苛柯棵磕颗科壳咳可渴克刻客课肯啃垦恳坑吭空恐孔控抠口扣寇枯哭窟苦酷库裤夸垮挎跨胯块筷侩快宽款匡筐狂框矿眶旷况亏盔岿窥葵奎魁傀馈愧溃坤昆捆困括扩廓阔垃拉喇蜡腊辣啦莱来赖蓝婪栏拦篮阑兰澜谰揽览懒缆烂滥琅榔狼廊郎朗浪捞劳牢老佬姥酪烙涝勒乐雷镭蕾磊累儡垒擂肋类泪棱楞冷厘梨犁黎篱狸离漓理李里鲤礼莉荔吏栗丽厉励砾历利傈例俐痢立粒沥隶力璃哩俩联莲连镰廉怜涟帘敛脸链恋炼练粮凉梁粱良两辆量晾亮谅撩聊僚疗燎寥辽潦了撂镣廖料列裂烈劣猎琳林磷霖临邻鳞淋凛赁吝拎玲菱零龄铃伶羚凌灵陵岭领另令溜琉榴硫馏留刘瘤流柳六龙聋咙笼窿隆垄拢陇楼娄搂篓漏陋芦卢颅庐炉掳卤虏鲁麓碌露路赂鹿潞禄录陆戮驴吕铝侣旅履屡缕虑氯律率滤绿峦挛孪滦卵乱掠略抡轮伦仑沦纶论萝螺罗逻锣箩骡裸落洛骆络妈麻玛码蚂马骂嘛吗埋买麦卖迈脉瞒馒蛮满蔓曼慢漫谩芒茫盲氓忙莽猫茅锚毛矛铆卯茂冒帽貌贸么玫枚梅酶霉煤没眉媒镁每美昧寐妹媚门闷们萌蒙檬盟锰猛梦孟眯醚靡糜迷谜弥米秘觅泌蜜密幂棉眠绵冕免勉娩缅面苗描瞄藐秒渺庙妙蔑灭民抿皿敏悯闽明螟鸣铭名命谬摸摹蘑模膜磨摩魔抹末莫墨默沫漠寞陌谋牟某拇牡亩姆母墓暮幕募慕木目睦牧穆拿哪呐钠那娜纳氖乃奶耐奈南男难囊挠脑恼闹淖呢馁内嫩能妮霓倪泥尼拟你匿腻逆溺蔫拈年碾撵捻念娘酿鸟尿捏聂孽啮镊镍涅您柠狞凝宁拧泞牛扭钮纽脓浓农弄奴努怒女暖虐疟挪懦糯诺哦欧鸥殴藕呕偶沤啪趴爬帕怕琶拍排牌徘湃派攀潘盘磐盼畔判叛乓庞旁耪胖抛咆刨炮袍跑泡呸胚培裴赔陪配佩沛喷盆砰抨烹澎彭蓬棚硼篷膨朋鹏捧碰坯砒霹批披劈琵毗啤脾疲皮匹痞僻屁譬篇偏片骗飘漂瓢票撇瞥拼频贫品聘乒坪苹萍平凭瓶评屏坡泼颇婆破魄迫粕剖扑铺仆莆葡菩蒲埔朴圃普浦谱曝瀑期欺栖戚妻七凄漆柒沏其棋奇歧畦崎脐齐旗祈祁骑起岂乞企启契砌器气迄弃汽泣讫掐洽牵扦钎铅千迁签仟谦乾黔钱钳前潜遣浅谴堑嵌欠歉枪呛腔羌墙蔷强抢橇锹敲悄桥瞧乔侨巧鞘撬翘峭俏窍切茄且怯窃钦侵亲秦琴勤芹擒禽寝沁青轻氢倾卿清擎晴氰情顷请庆琼穷秋丘邱球求囚酋泅趋区蛆曲躯屈驱渠取娶龋趣去圈颧权醛泉全痊拳犬券劝缺炔瘸却鹊榷确雀裙群然燃冉染瓤壤攘嚷让饶扰绕惹热壬仁人忍韧任认刃妊纫扔仍日戎茸蓉荣融熔溶容绒冗揉柔肉茹蠕儒孺如辱乳汝入褥软阮蕊瑞锐闰润若弱撒洒萨腮鳃塞赛三叁伞散桑嗓丧搔骚扫嫂瑟色涩森僧莎砂杀刹沙纱傻啥煞筛晒珊苫杉山删煽衫闪陕擅赡膳善汕扇缮墒伤商赏晌上尚裳梢捎稍烧芍勺韶少哨邵绍奢赊蛇舌舍赦摄射慑涉社设砷申呻伸身深娠绅神沈审婶甚肾慎渗声生甥牲升绳省盛剩胜圣师失狮施湿诗尸虱十石拾时什食蚀实识史矢使屎驶始式示士世柿事拭誓逝势是嗜噬适仕侍释饰氏市恃室视试收手首守寿授售受瘦兽蔬枢梳殊抒输叔舒淑疏书赎孰熟薯暑曙署蜀黍鼠属术述树束戍竖墅庶数漱恕刷耍摔衰甩帅栓拴霜双爽谁水睡税吮瞬顺舜说硕朔烁斯撕嘶思私司丝死肆寺嗣四伺似饲巳松耸怂颂送宋讼诵搜艘擞嗽苏酥俗素速粟僳塑溯宿诉肃酸蒜算虽隋随绥髓碎岁穗遂隧祟孙损笋蓑梭唆缩琐索锁所塌他它她塔獭挞蹋踏胎苔抬台泰酞太态汰坍摊贪瘫滩坛檀痰潭谭谈坦毯袒碳探叹炭汤塘搪堂棠膛唐糖倘躺淌趟烫掏涛滔绦萄桃逃淘陶讨套特藤腾疼誊梯剔踢锑提题蹄啼体替嚏惕涕剃屉天添填田甜恬舔腆挑条迢眺跳贴铁帖厅听烃汀廷停亭庭挺艇通桐酮瞳同铜彤童桶捅筒统痛偷投头透凸秃突图徒途涂屠土吐兔湍团推颓腿蜕褪退吞屯臀拖托脱鸵陀驮驼椭妥拓唾挖哇蛙洼娃瓦袜歪外豌弯湾玩顽丸烷完碗挽晚皖惋宛婉万腕汪王亡枉网往旺望忘妄威巍微危韦违桅围唯惟为潍维苇萎委伟伪尾纬未蔚味畏胃喂魏位渭谓尉慰卫瘟温蚊文闻纹吻稳紊问嗡翁瓮挝蜗涡窝我斡卧握沃巫呜钨乌污诬屋无芜梧吾吴毋武五捂午舞伍侮坞戊雾晤物勿务悟误昔熙析西硒矽晰嘻吸锡牺稀息希悉膝夕惜熄烯溪汐犀檄袭席习媳喜铣洗系隙戏细瞎虾匣霞辖暇峡侠狭下厦夏吓掀锨先仙鲜纤咸贤衔舷闲涎弦嫌显险现献县腺馅羡宪陷限线相厢镶香箱襄湘乡翔祥详想响享项巷橡像向象萧硝霄削哮嚣销消宵淆晓小孝校肖啸笑效楔些歇蝎鞋协挟携邪斜胁谐写械卸蟹懈泄泻谢屑薪芯锌欣辛新忻心信衅星腥猩惺兴刑型形邢行醒幸杏性姓兄凶胸匈汹雄熊休修羞朽嗅锈秀袖绣墟戌需虚嘘须徐许蓄酗叙旭序畜恤絮婿绪续轩喧宣悬旋玄选癣眩绚靴薛学穴雪血勋熏循旬询寻驯巡殉汛训讯逊迅压押鸦鸭呀丫芽牙蚜崖衙涯雅哑亚讶焉咽阉烟淹盐严研蜒岩延言颜阎炎沿奄掩眼衍演艳堰燕厌砚雁唁彦焰宴谚验殃央鸯秧杨扬佯疡羊洋阳氧仰痒养样漾邀腰妖瑶摇尧遥窑谣姚咬舀药要耀椰噎耶爷野冶也页掖业叶曳腋夜液一壹医揖铱依伊衣颐夷遗移仪胰疑沂宜姨彝椅蚁倚已乙矣以艺抑易邑屹亿役臆逸肄疫亦裔意毅忆义益溢诣议谊译异翼翌绎茵荫因殷音阴姻吟银淫寅饮尹引隐印英樱婴鹰应缨莹萤营荧蝇迎赢盈影颖硬映哟拥佣臃痈庸雍踊蛹咏泳涌永恿勇用幽优悠忧尤由邮铀犹油游酉有友右佑釉诱又幼迂淤于盂榆虞愚舆余俞逾鱼愉渝渔隅予娱雨与屿禹宇语羽玉域芋郁吁遇喻峪御愈欲狱育誉浴寓裕预豫驭鸳渊冤元垣袁原援辕园员圆猿源缘远苑愿怨院曰约越跃钥岳粤月悦阅耘云郧匀陨允运蕴酝晕韵孕匝砸杂栽哉灾宰载再在咱攒暂赞赃脏葬遭糟凿藻枣早澡蚤躁噪造皂灶燥责择则泽贼怎增憎曾赠扎喳渣札轧铡闸眨栅榨咋乍炸诈摘斋宅窄债寨瞻毡詹粘沾盏斩辗崭展蘸栈占战站湛绽樟章彰漳张掌涨杖丈帐账仗胀瘴障招昭找沼赵照罩兆肇召遮折哲蛰辙者锗蔗这浙珍斟真甄砧臻贞针侦枕疹诊震振镇阵蒸挣睁征狰争怔整拯正政帧症郑证芝枝支吱蜘知肢脂汁之织职直植殖执值侄址指止趾只旨纸志挚掷至致置帜峙制智秩稚质炙痔滞治窒中盅忠钟衷终种肿重仲众舟周州洲诌粥轴肘帚咒皱宙昼骤珠株蛛朱猪诸诛逐竹烛煮拄瞩嘱主著柱助蛀贮铸筑住注祝驻抓爪拽专砖转撰赚篆桩庄装妆撞壮状椎锥追赘坠缀谆准捉拙卓桌琢茁酌啄着灼浊兹咨资姿滋淄孜紫仔籽滓子自渍字鬃棕踪宗综总纵邹走奏揍租足卒族祖诅阻组钻纂嘴醉最罪尊遵昨左佐柞做作坐座致钟么为只凶准启板里雳余链泄";
        private const string traditionalChinese = "啊阿埃挨哎唉哀皚癌藹矮艾礙愛隘鞍氨安俺按暗岸胺案肮昂盎凹敖熬翺襖傲奧懊澳芭捌扒叭吧笆八疤巴拔跋靶把耙壩霸罷爸白柏百擺佰敗拜稗斑班搬扳般頒板版扮拌伴瓣半辦絆邦幫梆榜膀綁棒磅蚌鎊傍謗苞胞包褒剝薄雹保堡飽寶抱報暴豹鮑爆杯碑悲卑北輩背貝鋇倍狽備憊焙被奔苯本笨崩繃甭泵蹦迸逼鼻比鄙筆彼碧蓖蔽畢斃毖幣庇痹閉敝弊必辟壁臂避陛鞭邊編貶扁便變卞辨辯辮遍標彪膘表鼈憋別癟彬斌瀕濱賓擯兵冰柄丙秉餅炳病並玻菠播撥缽波博勃搏鉑箔伯帛舶脖膊渤泊駁捕蔔哺補埠不布步簿部怖擦猜裁材才財睬踩采彩菜蔡餐參蠶殘慚慘燦蒼艙倉滄藏操糙槽曹草廁策側冊測層蹭插叉茬茶查碴搽察岔差詫拆柴豺攙摻蟬饞讒纏鏟産闡顫昌猖場嘗常長償腸廠敞暢唱倡超抄鈔朝嘲潮巢吵炒車扯撤掣徹澈郴臣辰塵晨忱沈陳趁襯撐稱城橙成呈乘程懲澄誠承逞騁秤吃癡持匙池遲弛馳恥齒侈尺赤翅斥熾充沖蟲崇寵抽酬疇躊稠愁籌仇綢瞅醜臭初出櫥廚躇鋤雛滁除楚礎儲矗搐觸處揣川穿椽傳船喘串瘡窗幢床闖創吹炊捶錘垂春椿醇唇淳純蠢戳綽疵茨磁雌辭慈瓷詞此刺賜次聰蔥囪匆從叢湊粗醋簇促躥篡竄摧崔催脆瘁粹淬翠村存寸磋撮搓措挫錯搭達答瘩打大呆歹傣戴帶殆代貸袋待逮怠耽擔丹單鄲撣膽旦氮但憚淡誕彈蛋當擋黨蕩檔刀搗蹈倒島禱導到稻悼道盜德得的蹬燈登等瞪凳鄧堤低滴迪敵笛狄滌翟嫡抵底地蒂第帝弟遞締顛掂滇碘點典靛墊電佃甸店惦奠澱殿碉叼雕凋刁掉吊釣調跌爹碟蝶叠諜疊丁盯叮釘頂鼎錠定訂丟東冬董懂動棟侗恫凍洞兜抖鬥陡豆逗痘都督毒犢獨讀堵睹賭杜鍍肚度渡妒端短鍛段斷緞堆兌隊對墩噸蹲敦頓囤鈍盾遁掇哆多奪垛躲朵跺舵剁惰墮蛾峨鵝俄額訛娥惡厄扼遏鄂餓恩而兒耳爾餌洱二貳發罰筏伐乏閥法琺藩帆番翻樊礬釩繁凡煩反返範販犯飯泛坊芳方肪房防妨仿訪紡放菲非啡飛肥匪誹吠肺廢沸費芬酚吩氛分紛墳焚汾粉奮份忿憤糞豐封楓蜂峰鋒風瘋烽逢馮縫諷奉鳳佛否夫敷膚孵扶拂輻幅氟符伏俘服浮涪福袱弗甫撫輔俯釜斧脯腑府腐赴副覆賦複傅付阜父腹負富訃附婦縛咐噶嘎該改概鈣蓋溉幹甘杆柑竿肝趕感稈敢贛岡剛鋼缸肛綱崗港杠篙臯高膏羔糕搞鎬稿告哥歌擱戈鴿胳疙割革葛格蛤閣隔鉻個各給根跟耕更庚羹埂耿梗工攻功恭龔供躬公宮弓鞏汞拱貢共鈎勾溝苟狗垢構購夠辜菇咕箍估沽孤姑鼓古蠱骨谷股故顧固雇刮瓜剮寡挂褂乖拐怪棺關官冠觀管館罐慣灌貫光廣逛瑰規圭矽歸龜閨軌鬼詭癸桂櫃跪貴劊輥滾棍鍋郭國果裹過哈骸孩海氦亥害駭酣憨邯韓含涵寒函喊罕翰撼捍旱憾悍焊汗漢夯杭航壕嚎豪毫郝好耗號浩呵喝荷菏核禾和何合盒貉閡河涸赫褐鶴賀嘿黑痕很狠恨哼亨橫衡恒轟哄烘虹鴻洪宏弘紅喉侯猴吼厚候後呼乎忽瑚壺葫胡蝴狐糊湖弧虎唬護互滬戶花嘩華猾滑畫劃化話槐徊懷淮壞歡環桓還緩換患喚瘓豢煥渙宦幻荒慌黃磺蝗簧皇凰惶煌晃幌恍謊灰揮輝徽恢蛔回毀悔慧卉惠晦賄穢會燴彙諱誨繪葷昏婚魂渾混豁活夥火獲或惑霍貨禍擊圾基機畸稽積箕肌饑迹激譏雞姬績緝吉極棘輯籍集及急疾汲即嫉級擠幾脊己薊技冀季伎祭劑悸濟寄寂計記既忌際妓繼紀嘉枷夾佳家加莢頰賈甲鉀假稼價架駕嫁殲監堅尖箋間煎兼肩艱奸緘繭檢柬堿鹼揀撿簡儉剪減薦檻鑒踐賤見鍵箭件健艦劍餞漸濺澗建僵姜將漿江疆蔣槳獎講匠醬降蕉椒礁焦膠交郊澆驕嬌嚼攪鉸矯僥腳狡角餃繳絞剿教酵轎較叫窖揭接皆稭街階截劫節莖睛晶鯨京驚精粳經井警景頸靜境敬鏡徑痙靖竟競淨炯窘揪究糾玖韭久灸九酒廄救舊臼舅咎就疚鞠拘狙疽居駒菊局咀矩舉沮聚拒據巨具距踞鋸俱句懼炬劇捐鵑娟倦眷卷絹撅攫抉掘倔爵桔傑捷睫竭潔結解姐戒藉芥界借介疥誡屆巾筋斤金今津襟緊錦僅謹進靳晉禁近燼浸盡勁荊兢覺決訣絕均菌鈞軍君峻俊竣浚郡駿喀咖卡咯開揩楷凱慨刊堪勘坎砍看康慷糠扛抗亢炕考拷烤靠坷苛柯棵磕顆科殼咳可渴克刻客課肯啃墾懇坑吭空恐孔控摳口扣寇枯哭窟苦酷庫褲誇垮挎跨胯塊筷儈快寬款匡筐狂框礦眶曠況虧盔巋窺葵奎魁傀饋愧潰坤昆捆困括擴廓闊垃拉喇蠟臘辣啦萊來賴藍婪欄攔籃闌蘭瀾讕攬覽懶纜爛濫琅榔狼廊郎朗浪撈勞牢老佬姥酪烙澇勒樂雷鐳蕾磊累儡壘擂肋類淚棱楞冷厘梨犁黎籬狸離漓理李裏鯉禮莉荔吏栗麗厲勵礫曆利傈例俐痢立粒瀝隸力璃哩倆聯蓮連鐮廉憐漣簾斂臉鏈戀煉練糧涼梁粱良兩輛量晾亮諒撩聊僚療燎寥遼潦了撂鐐廖料列裂烈劣獵琳林磷霖臨鄰鱗淋凜賃吝拎玲菱零齡鈴伶羚淩靈陵嶺領另令溜琉榴硫餾留劉瘤流柳六龍聾嚨籠窿隆壟攏隴樓婁摟簍漏陋蘆盧顱廬爐擄鹵虜魯麓碌露路賂鹿潞祿錄陸戮驢呂鋁侶旅履屢縷慮氯律率濾綠巒攣孿灤卵亂掠略掄輪倫侖淪綸論蘿螺羅邏鑼籮騾裸落洛駱絡媽麻瑪碼螞馬罵嘛嗎埋買麥賣邁脈瞞饅蠻滿蔓曼慢漫謾芒茫盲氓忙莽貓茅錨毛矛鉚卯茂冒帽貌貿麽玫枚梅酶黴煤沒眉媒鎂每美昧寐妹媚門悶們萌蒙檬盟錳猛夢孟眯醚靡糜迷謎彌米秘覓泌蜜密冪棉眠綿冕免勉娩緬面苗描瞄藐秒渺廟妙蔑滅民抿皿敏憫閩明螟鳴銘名命謬摸摹蘑模膜磨摩魔抹末莫墨默沫漠寞陌謀牟某拇牡畝姆母墓暮幕募慕木目睦牧穆拿哪呐鈉那娜納氖乃奶耐奈南男難囊撓腦惱鬧淖呢餒內嫩能妮霓倪泥尼擬你匿膩逆溺蔫拈年碾攆撚念娘釀鳥尿捏聶孽齧鑷鎳涅您檸獰凝甯擰濘牛扭鈕紐膿濃農弄奴努怒女暖虐瘧挪懦糯諾哦歐鷗毆藕嘔偶漚啪趴爬帕怕琶拍排牌徘湃派攀潘盤磐盼畔判叛乓龐旁耪胖抛咆刨炮袍跑泡呸胚培裴賠陪配佩沛噴盆砰抨烹澎彭蓬棚硼篷膨朋鵬捧碰坯砒霹批披劈琵毗啤脾疲皮匹痞僻屁譬篇偏片騙飄漂瓢票撇瞥拼頻貧品聘乒坪蘋萍平憑瓶評屏坡潑頗婆破魄迫粕剖撲鋪仆莆葡菩蒲埔樸圃普浦譜曝瀑期欺棲戚妻七淒漆柒沏其棋奇歧畦崎臍齊旗祈祁騎起豈乞企啓契砌器氣迄棄汽泣訖掐洽牽扡釺鉛千遷簽仟謙乾黔錢鉗前潛遣淺譴塹嵌欠歉槍嗆腔羌牆薔強搶橇鍬敲悄橋瞧喬僑巧鞘撬翹峭俏竅切茄且怯竊欽侵親秦琴勤芹擒禽寢沁青輕氫傾卿清擎晴氰情頃請慶瓊窮秋丘邱球求囚酋泅趨區蛆曲軀屈驅渠取娶齲趣去圈顴權醛泉全痊拳犬券勸缺炔瘸卻鵲榷確雀裙群然燃冉染瓤壤攘嚷讓饒擾繞惹熱壬仁人忍韌任認刃妊紉扔仍日戎茸蓉榮融熔溶容絨冗揉柔肉茹蠕儒孺如辱乳汝入褥軟阮蕊瑞銳閏潤若弱撒灑薩腮鰓塞賽三三傘散桑嗓喪搔騷掃嫂瑟色澀森僧莎砂殺刹沙紗傻啥煞篩曬珊苫杉山刪煽衫閃陝擅贍膳善汕扇繕墒傷商賞晌上尚裳梢捎稍燒芍勺韶少哨邵紹奢賒蛇舌舍赦攝射懾涉社設砷申呻伸身深娠紳神沈審嬸甚腎慎滲聲生甥牲升繩省盛剩勝聖師失獅施濕詩屍虱十石拾時什食蝕實識史矢使屎駛始式示士世柿事拭誓逝勢是嗜噬適仕侍釋飾氏市恃室視試收手首守壽授售受瘦獸蔬樞梳殊抒輸叔舒淑疏書贖孰熟薯暑曙署蜀黍鼠屬術述樹束戍豎墅庶數漱恕刷耍摔衰甩帥栓拴霜雙爽誰水睡稅吮瞬順舜說碩朔爍斯撕嘶思私司絲死肆寺嗣四伺似飼巳松聳慫頌送宋訟誦搜艘擻嗽蘇酥俗素速粟僳塑溯宿訴肅酸蒜算雖隋隨綏髓碎歲穗遂隧祟孫損筍蓑梭唆縮瑣索鎖所塌他它她塔獺撻蹋踏胎苔擡台泰酞太態汰坍攤貪癱灘壇檀痰潭譚談坦毯袒碳探歎炭湯塘搪堂棠膛唐糖倘躺淌趟燙掏濤滔縧萄桃逃淘陶討套特藤騰疼謄梯剔踢銻提題蹄啼體替嚏惕涕剃屜天添填田甜恬舔腆挑條迢眺跳貼鐵帖廳聽烴汀廷停亭庭挺艇通桐酮瞳同銅彤童桶捅筒統痛偷投頭透凸禿突圖徒途塗屠土吐兔湍團推頹腿蛻褪退吞屯臀拖托脫鴕陀馱駝橢妥拓唾挖哇蛙窪娃瓦襪歪外豌彎灣玩頑丸烷完碗挽晚皖惋宛婉萬腕汪王亡枉網往旺望忘妄威巍微危韋違桅圍唯惟爲濰維葦萎委偉僞尾緯未蔚味畏胃喂魏位渭謂尉慰衛瘟溫蚊文聞紋吻穩紊問嗡翁甕撾蝸渦窩我斡臥握沃巫嗚鎢烏汙誣屋無蕪梧吾吳毋武五捂午舞伍侮塢戊霧晤物勿務悟誤昔熙析西硒矽晰嘻吸錫犧稀息希悉膝夕惜熄烯溪汐犀檄襲席習媳喜銑洗系隙戲細瞎蝦匣霞轄暇峽俠狹下廈夏嚇掀鍁先仙鮮纖鹹賢銜舷閑涎弦嫌顯險現獻縣腺餡羨憲陷限線相廂鑲香箱襄湘鄉翔祥詳想響享項巷橡像向象蕭硝霄削哮囂銷消宵淆曉小孝校肖嘯笑效楔些歇蠍鞋協挾攜邪斜脅諧寫械卸蟹懈泄瀉謝屑薪芯鋅欣辛新忻心信釁星腥猩惺興刑型形邢行醒幸杏性姓兄凶胸匈洶雄熊休修羞朽嗅鏽秀袖繡墟戌需虛噓須徐許蓄酗敘旭序畜恤絮婿緒續軒喧宣懸旋玄選癬眩絢靴薛學穴雪血勳熏循旬詢尋馴巡殉汛訓訊遜迅壓押鴉鴨呀丫芽牙蚜崖衙涯雅啞亞訝焉咽閹煙淹鹽嚴研蜒岩延言顔閻炎沿奄掩眼衍演豔堰燕厭硯雁唁彥焰宴諺驗殃央鴦秧楊揚佯瘍羊洋陽氧仰癢養樣漾邀腰妖瑤搖堯遙窯謠姚咬舀藥要耀椰噎耶爺野冶也頁掖業葉曳腋夜液一壹醫揖銥依伊衣頤夷遺移儀胰疑沂宜姨彜椅蟻倚已乙矣以藝抑易邑屹億役臆逸肄疫亦裔意毅憶義益溢詣議誼譯異翼翌繹茵蔭因殷音陰姻吟銀淫寅飲尹引隱印英櫻嬰鷹應纓瑩螢營熒蠅迎贏盈影穎硬映喲擁傭臃癰庸雍踴蛹詠泳湧永恿勇用幽優悠憂尤由郵鈾猶油遊酉有友右佑釉誘又幼迂淤于盂榆虞愚輿余俞逾魚愉渝漁隅予娛雨與嶼禹宇語羽玉域芋郁籲遇喻峪禦愈欲獄育譽浴寓裕預豫馭鴛淵冤元垣袁原援轅園員圓猿源緣遠苑願怨院曰約越躍鑰嶽粵月悅閱耘雲鄖勻隕允運蘊醞暈韻孕匝砸雜栽哉災宰載再在咱攢暫贊贓髒葬遭糟鑿藻棗早澡蚤躁噪造皂竈燥責擇則澤賊怎增憎曾贈紮喳渣劄軋鍘閘眨柵榨咋乍炸詐摘齋宅窄債寨瞻氈詹粘沾盞斬輾嶄展蘸棧占戰站湛綻樟章彰漳張掌漲杖丈帳賬仗脹瘴障招昭找沼趙照罩兆肇召遮折哲蟄轍者鍺蔗這浙珍斟真甄砧臻貞針偵枕疹診震振鎮陣蒸掙睜征猙爭怔整拯正政幀症鄭證芝枝支吱蜘知肢脂汁之織職直植殖執值侄址指止趾只旨紙志摯擲至致置幟峙制智秩稚質炙痔滯治窒中盅忠鍾衷終種腫重仲衆舟周州洲謅粥軸肘帚咒皺宙晝驟珠株蛛朱豬諸誅逐竹燭煮拄矚囑主著柱助蛀貯鑄築住注祝駐抓爪拽專磚轉撰賺篆樁莊裝妝撞壯狀椎錐追贅墜綴諄准捉拙卓桌琢茁酌啄著灼濁茲咨資姿滋淄孜紫仔籽滓子自漬字鬃棕蹤宗綜總縱鄒走奏揍租足卒族祖詛阻組鑽纂嘴醉最罪尊遵昨左佐柞做作坐座緻鐘麼為隻兇準啟闆裡靂餘鍊洩";
        #endregion
    }

    internal class Api
    {
        [DllImport("msvcrt.dll", SetLastError = true)]
        private static extern int _mkdir(string path);

        public static int CreateDirectory(string path)
        {
            return _mkdir(path);
        }
    }
}
